分页爬取斋藤飞鸟图片
程序员文章站
2022-03-29 20:17:58
#简单爬取斋藤飞鸟图片import requestsimport timefrom lxml import etree# from requests.exceptions import RequestExceptiondef get_one_page(url): headers={ 'User - ......
#简单爬取斋藤飞鸟图片
import requests
import time
from lxml import etree
# from requests.exceptions import requestexception
def get_one_page(url):
headers={
'user - agent':'mozilla / 5.0(windows nt 10.0;win64;x64) applewebkit / 537.36(khtml, likegecko) chrome / 75.0.3770.100safari / 537.36',
}
r=requests.get(url,headers=headers)
# if r.status_code==200:
# return r.text
# return none
# except recursionerror:
# return none
contend=r.text
html=etree.html(contend)
src_list=html.xpath('//img/@src')
alt_list=html.xpath('//div[@class="prop"]/text()')
for src,alt in zip(src_list,alt_list):
filename=src[48:]
print('正在爬取第%d页'%(d/30))
print('正在保存图片'+filename)
r = requests.get(src,headers=headers)
with open('斋藤飞鸟图片//'+filename,'wb') as f:
f.write(r.content)
def main(d):
url='https://movie.douban.com/celebrity/1329640/photos/?type=c&start=%d&sortby=like&size=a&subtype=a' %(d)
# print(r.text)
print(get_one_page(url))
if __name__=='__main__':
d = 0
while true:
d = d + 30
if d >= 500:
break
main(d)
time.sleep(0.5)