当前位置: 首页 > news >正文

Python异步爬虫批量下载图片-协程

import aiofiles
import aiohttp
import asyncio
import requests
from lxml import etree
from aiohttp import TCPConnectorclass Spider:def __init__(self, value):# 起始urlself.start_url = value# 下载单个图片@staticmethodasync def download_one(url):name = url[0].split("/")[-1][:-4]print("开始下载", url, name)headers = {'Host': 'file.jiutuvip.com','User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, ''like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8','Accept-Language': 'zh-CN,zh;q=0.9','Accept-Encoding': 'gzip, deflate, br, zstd','Connection': 'keep-alive','Upgrade-Insecure-Requests': '1','Sec-Fetch-Dest': 'document','Sec-Fetch-Mode': 'navigate','Sec-Fetch-Site': 'none','Sec-Fetch-User': '?1','TE': 'trailers'}# 发送网络请求async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:async with session.get(url=url[0], headers=headers) as resp:  # 相当于 requests.get(url=url[0], headers=head)# await resp.text() => resp.textcontent = await resp.content.read()  # => resp.content# 写入文件async with aiofiles.open('./imgs/' + name + '.webp', "wb") as f:await f.write(content)print("下载完毕")# 获取图片的urlasync def download(self, href_list):for href in href_list:async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:async with session.get(url=href) as child_res:html = await child_res.text()child_tree = etree.HTML(html)src = child_tree.xpath("//div[@class='article-body cate-6']/a/img/@src")  # 选手图片地址 url 列表await self.download_one(src)# 获取图片详情urlasync def get_img_url(self, html_url):async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:async with session.get(url=html_url) as resp:html = await resp.text()tree = etree.HTML(html)href_list = tree.xpath("//div[@class='uk-container']/ul/li/a/@href")  # 选手详情页 url 列表print(href_list)await self.download(href_list)# 页面总页数@staticmethoddef get_html_url(url):page = 2response = requests.get(url=url)response.encoding = "utf-8"tree = etree.HTML(response.text)total_page = tree.xpath("//*[@class='pages']/a[12]/text()")  # 页面总页数print(total_page)html_url_list = []while page <= 4:  # int(total_page[0])  # 只取第 2、3、4 页next_url = f"https://www.yeitu.com/meinv/xinggan/{page}.html"html_url_list.append(next_url)page += 1print(html_url_list)return html_url_listasync def main(self):# 拿到每页url列表html_url_list = self.get_html_url(url=self.start_url)  # url列表tasks = []for html_url in html_url_list:t = asyncio.create_task(self.get_img_url(html_url))  # 创建任务tasks.append(t)await asyncio.wait(tasks)if __name__ == '__main__':url = "https://www.yeitu.com/meinv/xinggan/"sp = Spider(url)# loop = asyncio.get_event_loop()# loop = asyncio.new_event_loop()# asyncio.set_event_loop(loop)# loop.run_until_complete(sp.main())asyncio.run(sp.main())

http://www.lryc.cn/news/369277.html

相关文章:

  • 力扣 42. 接雨水
  • Elastic Platform 8.14:ES|QL 正式发布、静态加密和向量搜索优化
  • UE4获取动画序列资产的动画时长
  • win10怎么截图?电脑截图的3个方法分享
  • 无线领夹麦克风哪个品牌性价比高?推荐领夹麦克风性价比最高品牌
  • C语言----深入理解指针(5)
  • Ansible——cron模块
  • 保存图片奇怪的bug
  • 【Go语言精进之路】构建高效Go程序:了解map实现原理并高效使用
  • 【机器人和人工智能——自主巡航赛项】进阶篇
  • [大师C语言(第二十五篇)]C语言字符串探秘
  • xLua(一) 环境安装笔记
  • Python基础教程(十一):数据结构汇总梳理
  • 制造型企业图纸泄露问题,如何从根源解决核心文件资料泄露问题?
  • 英伟达最新GPU和互联路线图分析
  • Github 2024-06-10 开源项目日报 Top10
  • 前后端分离项目中Spring Boot返回的时间与前端相差8个小时
  • stm32MP135裸机编程:使用USB/UART烧录程序到SD卡并从SD卡启动点亮一颗LED灯
  • 【NoSQL数据库】Redis Cluster集群(含redis集群扩容脚本)
  • 重邮计算机网络803-(2)物理层
  • uniapp使用webview内嵌H5的注意事项
  • 现代 C++的高效并发编程模式
  • 汇编语言作业(五)
  • 收音机的原理笔记
  • 排序算法案例
  • 时间序列评价指标
  • Docker:安装 Orion-Visor 服务器运维的技术指南
  • HarmonyOS Next 系列之底部标签栏TabBar实现(三)
  • mac怎么录制屏幕?这2个方法你值得拥有
  • 爱德华三坐标软件ACdmis.AC-dmis密码注册机