【我的python之路】-记录人生第一只python爬虫-爬图小蜘蛛~
2022/1/13 17:05:04
本文主要是介绍【我的python之路】-记录人生第一只python爬虫-爬图小蜘蛛~,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import requests import re import json from lxml import etree listurl=[] listres=[] for unum in range(1,3): url = f'https://desk.zol.com.cn/pc/{unum}.html' listurl.append(url) for ur in listurl: furl = requests.get(ur) ets = etree.HTML(furl.text) result = ets.xpath('//ul[@class="pic-list2 clearfix"]/li/a[@class="pic"]/@href') for i in result: i = 'https://desk.zol.com.cn/' + i if i.endswith('.exe'): continue listres.append(i) for item in listres: url = item urlstr = requests.get(url) deskPic = re.compile(r'var deskPicArr.*?=(?P<deskPicArr>.*?);', re.S) urlstrlist = deskPic.search(urlstr.text) liststr = urlstrlist.group('deskPicArr') aaa = json.loads(liststr) for item in aaa['list']: oriSize = item['oriSize'] imgsrc = item['imgsrc'] imgsrc = imgsrc.replace('##SIZE##', oriSize) name = imgsrc.split('/')[-1] #取图片最后一个/后的名称为图片名,保存到本地地址 resp_img = requests.get(imgsrc) with open(f'C:\\Users\wu\Pictures\Saved Pictures\\{name}', mode='wb') as fa: fa.write(resp_img.content) break print('下载完成!')
这篇关于【我的python之路】-记录人生第一只python爬虫-爬图小蜘蛛~的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型