#coding:utf-8
from lxml import etree
import requests
import urllib.request
import progressbar
header={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'+
'Chrome/67.0.3396.87 Safari/537.36'
}
word=input('请输入搜索的内容:')
urls=['https://www.pexels.com/search/'+word+'/?page={}'.format(str(i))
for i in range(11,12)]
path='E://G/'
for url in urls :
res=requests.get(url)
selector=etree.HTML(res.text)
second_urls=selector.xpath('//div[@class="photos"]/article')
print(len(second_urls))
for second_url in second_urls:
new_url=second_url.xpath('a[1]/@href')[0]
new_url='https://www.pexels.com'+str(new_url)
#print(new_url)
new_res=requests.get(new_url)
new_selector=etree.HTML(new_res.text)
new_second_urls=new_selector.xpath('//a[@class="btn__primary js-download"]')
img_src = new_second_urls[0].xpath('@href')[0]
img_src=img_src.split('?')[0]
data=requests.get(img_src,headers=header)
total_length = int(data.headers.get("Content-Length"))
with open(path+img_src.split('/')[-1], 'wb') as f:
widgets = ['Progress: ', progressbar.Percentage(), ' ',progressbar.Bar(marker='#', left='[', right=']'),' ', progressbar.ETA(), ' ', progressbar.FileTransferSpeed()]
pbar = progressbar.ProgressBar(widgets=widgets, maxval=total_length).start()
count=0
for chunk in data.iter_content(chunk_size=1):
if chunk:
count=count+1
f.write(chunk)
f.flush()
pbar.update(count)
pbar.finish()
这里首先输入搜索关键字,输出每页图片张数,打印下载进度条