代码福利新手爬取汇总

新手python代码分享:爬取每期福利汇总第三页的三次元图片

网络资讯 2023-01-19 22:11:58 24

导读

importrequestsfromlxmlimportetreeday0=150whileday0<167:day1=str(day0)url1='https://fuliba2021.net/2021'+day1+'.html/3'headers={"User……

importrequests

fromlxml importetree

day0 = 150whileday0 < 167:

day1 = str(day0)

url1 = 'https://fuliba2021.net/2021'+day1+'.html/3' headers = {

"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4621.0 Safari/537.36" }

resp = requests.get(url=url1,headers= headers)

resp.encoding = 'utf-8' resp_text = resp.text

resp.close()

tree = etree.HTML(resp_text)

imgs = tree.xpath("/html/body/section/div[1]/div/article/p")

forimg inimgs:

result1 = img.xpath("./img/@src")

forul inresult1:

img_resp = requests.get(url= ul)

img_resp_content = img_resp.content

img_name = ul.split('/')[-1]

withopen('C:imgpian'+img_name,mode= 'wb') asf:

f.write(img_resp_content)

print('over',img_name)

print('第'+day1+'期图片下载完毕')

day0 = day0 +1else:

print('好像没有更多期了')

print('下载完毕!!!')