获取必应高清壁纸
import requests
import re
import os
from lxml import etree
dirName = '必应壁纸'
if not os.path.exists(dirName):
os.mkdir(dirName)
#必应图片网页地址
url = 'https://bing.ioliu.cn//?p=%d'
#图片的张数
number = 0
for page in range(1,6):
if page == 1:
new_url = 'https://bing.ioliu.cn//?p=1'
else:
new_url = format(url%page) # 表示非第一页的url
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'
}
html = requests.get(new_url,headers=headers).text # 下载网页
etree_html = etree.HTML(html) # 构造xpath的解析对象
img_src_list = etree_html.xpath('//img/@src') # 获取图片地址列表
for img_src in img_src_list:
img_src = re.sub('640x480','1920x1080',img_src) # 获取1920x1080高清壁纸
img_name = re.search('bing/(w+?)_',img_src) # 获取图片对应名称
img = requests.get(img_src,headers=headers).content
number+=1
print('正在下载第{}张图片,地址为:{}'.format(number,img_src))
img_name = '必应壁纸\{}.jpg'.format(img_name.group(1)) #保存至指定位置
with open(img_name,'wb') as save_img:
save_img.write(img)
print('下载完毕!!!')