本文内容
- 系统分析目标网页
- html标签数据解析方法
- 海量图片数据一键保存
环境介绍
- python 3.8
- pycharm
模块使用
- requests >>> pip install requests
- parsel >>> pip install parsel
- time 时间模块 记录运行时间
通用爬虫
导入模块
import requests # 数据请求模块 第三方模块 pip install requests
import parsel # 数据解析模块 第三方模块 pip install parsel
import re # 正则表达式模块
请求数据
url = f'https://fabiaoqing.com/biaoqing/lists/page/{page}html'
headers \= {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
}
response \= requests.get(url=url, headers=headers)
# <Response \[200\]> response 对象 200状态码 表示请求成功
解析数据
解析速度 bs4 解析速度会慢一些,如果你想要对于字符串数据内容,直接取值,只能正则表达式
selector = parsel.Selector(response.text) # 把获取下来html字符串数据内容 转成 selector 对象
title\_list = selector.css('.ui.image.lazy::attr(title)').getall()
img\_list \= selector.css('.ui.image.lazy::attr(data-original)').getall()
# 把获取下来的这两个列表 提取里面元素 一一提取出来
# 提取列表元素 for循环 遍历
for title, img\_url in zip(title\_list, img\_list):title \= re.sub(r'\[\\/:\*?"<>|\\n\]', '\_', title)# 名字太长 报错img\_name = img\_url.split('.')\[-1\] # 通过split() 字符串分割的方法 根据列表索引位置取值img\_content = requests.get(url=img\_url).content # 获取图片的二进制数据内容
保存数据
with open('img\\\\' + title + '.' + img\_name, mode='wb') as f:f.write(img\_content)
print(title)
共耗时:61秒
多线程爬虫
发送求情
def get\_response(html\_url):headers \= {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'}response \= requests.get(url=html\_url, headers=headers)return response
获取图片url地址,以及图片名字
def get\_img\_info(html\_url):response \= get\_response(html\_url)selector \= parsel.Selector(response.text) # 把获取下来html字符串数据内容 转成 selector 对象title\_list = selector.css('.ui.image.lazy::attr(title)').getall()img\_list \= selector.css('.ui.image.lazy::attr(data-original)').getall()zip\_data \= zip(title\_list, img\_list)return zip\_data
保存数据
def save(title, img\_url):title \= re.sub(r'\[\\/:\*?"<>|\\n\]', '\_', title)# 名字太长 报错img\_name = img\_url.split('.')\[-1\] # 通过split() 字符串分割的方法 根据列表索引位置取值img\_content = requests.get(url=img\_url).content # 获取图片的二进制数据内容with open('img\\\\' + title + '.' + img\_name, mode='wb') as f:f.write(img\_content)print(title)
主函数
def main(html\_url):zip\_data \= get\_img\_info(html\_url)for title, img\_url in zip\_data:save(title, img\_url)
入口
if \_\_name\_\_ == '\_\_main\_\_':start\_time \= time.time()exe \= concurrent.futures.ThreadPoolExecutor(max\_workers=10)for page in range(1, 11):url \= f'https://fabiaoqing.com/biaoqing/lists/page/{page}html'exe.submit(main, url)exe.shutdown()end\_time \= time.time()use\_time \= int(end\_time - start\_time)print('程序耗时: ', use\_time)
共耗时:19秒
这里我为大家准备了一份针对零基础的Python学习资料,有兴趣的同学可以看看哦。
「 Python经验分享 」
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后给大家免费分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
这份完整版的Python全套学习资料已经上传CSDN
朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】。