本文内容
- 系统分析目标网页
- html标签数据解析方法
- 海量图片数据一键保存
- python 3.8
- pycharm
- requests >>> pip install requests
- parsel >>> pip install parsel
- time 时间模块 记录运行时间
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)【不会吧,学过爬虫连这个网站都爬不了?那Python岂不是白学了】

文章插图
共耗时: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 = https://tazarkount.com/read/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秒对于本篇文章有疑问的同学可以加【资料白嫖、解答交流群:1039649593】
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
