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

Python 异步爬虫(aiohttp)高效抓取新闻数据

一、异步爬虫的优势

在传统的同步爬虫中,爬虫在发送请求后会阻塞等待服务器响应,直到收到响应后才会继续执行后续操作。这种模式在面对大量请求时,会导致大量的时间浪费在等待响应上,爬取效率较低。而异步爬虫则等待可以在服务器响应的同时,继续执行其他任务,大大提高了爬取效率。

<font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">aiohttp</font> 是一个支持异步请求的 Python 库,它基于 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">asyncio</font> 框架,可以实现高效的异步网络请求。使用 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">aiohttp</font> 构建异步爬虫,可以在短时间内发起大量请求,同时处理多个响应,从而实现高效的数据抓取。

二、环境准备

在开始编写异步爬虫之前,需要确保已经安装了 Python 以及 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">aiohttp</font> 库。如果尚未安装 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">aiohttp</font>

此外,为了更好地处理 HTML 内容,我们还需要安装 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">beautifulsoup4</font> 库,用于解析 HTML 文档:

三、构建异步爬虫

1. 初始化异步爬虫

首先,我们需要创建一个异步函数来初始化爬虫。在这个函数中,我们将设置异步会话(<font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">aiohttp.ClientSession</font>),用于发送网络请求。

import aiohttp
import asyncio
from bs4 import BeautifulSoupasync def fetch(session, url):"""异步发送 GET 请求:param session: aiohttp.ClientSession 对象:param url: 请求的 URL:return: 响应的 HTML 内容"""async with session.get(url) as response:return await response.text()

2. 解析新闻数据

在获取到新闻页面的 HTML 内容后,我们需要使用 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">BeautifulSoup</font> 对其进行解析,提取出新闻的标题、链接等关键信息。

def parse_news):
(html    """解析 HTML 内容,提取新闻信息:param html: 新闻页面的 HTML 内容:return: 新闻信息列表"""soup = BeautifulSoup(html, 'html.parser')news_list = []# 假设新闻标题在 <h2> 标签中,新闻链接在 <a> 标签的 href 属性中for item in soup.find_all('h2'):title = item.get_text()link = item.find('a')['href']news_list.append({'title': title, 'link': link})return news_list

3. 异步任务调度

为了实现高效的异步爬取,我们需要将多个请求任务调度到事件循环中。通过创建多个异步任务,并将它们添加到事件循环中,可以同时发起多个请求。

async def main():url = 'https://example.com/news'  # 新闻网站的 URLasync with aiohttp.ClientSession() as session:html = await fetch(session, url)news_list = parse_news(html)for news in news_list:print(news)if __name__ == '__main__':asyncio.run(main())

4. 多任务并发

在实际应用中,我们通常需要爬取多个新闻页面。为了提高效率,可以使用 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">asyncio.gather</font> 方法并发执行多个异步任务。

async def fetch_news(session, url):html = await fetch(session, url)return parse_news(html)async def main():urls = ['https://example.com/news/page1','https://example.com/news/page2','https://example.com/news/page3',# 更多新闻页面的 URL]async with aiohttp.ClientSession() as session:tasks = [fetch_news(session, url) for url in urls]all_news = await asyncio.gather(*tasks)for news_list in all_news:for news in news_list:print(news)if __name__ == '__main__':asyncio.run(main())

四、优化与注意事项

1. 错误处理

在爬取过程中,可能会遇到各种错误,如网络请求超时、服务器返回错误状态码等。为了保证爬虫的稳定性,需要对这些错误进行处理。

async def fetch(session, url):try:async with session.get(url, timeout=10) as response:  # 设置请求超时时间response.raise_for_status()  # 检查响应状态码return await response.text()except asyncio.TimeoutError:print(f"请求超时:{url}")except aiohttp.ClientResponseError as e:print(f"请求错误:{url}, 状态码:{e.status}")except Exception as e:print(f"未知错误:{url}, 错误信息:{e}")

2. 遵守网站规则

在爬取新闻数据时,需要遵守目标网站的 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">robots.txt</font> 文件规定,避免对网站造成过大压力。同时,合理设置请求间隔,避免被网站封禁。

3. 数据存储

爬取到的新闻数据可以存储到本地文件、数据库或云存储中,以便后续进行分析和处理。

五、总结

本文介绍了如何使用 Python 的 <font style="color:rgba(0, 0, 0, 0.9);background-color:rgba(0, 0, 0, 0.03);">aiohttp</font> 库构建异步爬虫,高效抓取新闻数据。通过异步请求和并发任务调度,可以显著提高爬取效率。在实际应用中,还需要注意错误处理、遵守网站规则以及数据存储等问题。希望本文能够帮助读者更好地理解和应用 Python 异步爬虫技术。

六、完整代码

import aiohttp
import asyncio
from bs4 import BeautifulSoup# 代理配置
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
proxyUrl = f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"async def fetch(session, url):try:async with session.get(url, timeout=10, proxy=proxyUrl) as response:response.raise_for_status()return await response.text()except asyncio.TimeoutError:print(f"请求超时:{url}")except aiohttp.ClientResponseError as e:print(f"请求错误:{url}, 状态码:{e.status}")except Exception as e:print(f"未知错误:{url}, 错误信息:{e}")def parse_news(html):soup = BeautifulSoup(html, 'html.parser')news_list = []for item in soup.find_all('h2'):title = item.get_text()link = item.find('a')['href'] if item.find('a') else Noneif title and link:news_list.append({'title': title, 'link': link})return news_listasync def fetch_news(session, url):html = await fetch(session, url)if html:return parse_news(html)return []async def main():urls = ['https://example.com/news/page1','https://example.com/news/page2','https://example.com/news/page3',# 更多新闻页面的 URL]# 配置代理认证proxy_auth = aiohttp.BasicAuth(proxyUser, proxyPass)conn = aiohttp.TCPConnector(limit=10)  # 限制连接数async with aiohttp.ClientSession(connector=conn) as session:tasks = [fetch_news(session, url) for url in urls]all_news = await asyncio.gather(*tasks)for news_list in all_news:for news in news_list:print(news)if __name__ == '__main__':asyncio.run(main())
http://www.dtcms.com/a/265541.html

相关文章:

  • 前端开发中的 Base64 图片革命:从链接到嵌入的性能优化
  • Go爬虫实时性能监控方案
  • 利用人名语言分类案例演示RNN、LSTM和GRU的区别(基于PyTorch)
  • 【学习线路】机器学习线路概述与内容关键点说明
  • git 中删除提交历史
  • 闲庭信步使用SV搭建图像测试平台:第二十七课——图像的腐蚀
  • Windows DOS CMD 100
  • PostgreSQL-XL之 序列(Sequence)
  • 深度学习2(逻辑回归+损失函数+梯度下降)
  • 基于Spring Boot + MyBatis-Plus + Thymeleaf的评论管理系统深度解析
  • Spring Boot + Screw 一键生成数据库设计文档
  • GitHub 解码指南:用 AI 赋能,五步快速掌握任意开源项目
  • WordPress 站点漏洞利用:数据库恶意注入与多重感染的案例分析
  • 大数据环境搭建指南:基于 Docker 构建 Hadoop、Hive、HBase 等服务
  • 如何在Jupyter notebook中删除内核以及添加内核
  • 大数据救公益:数字时代下的社会力量如何玩转“数据+善意”
  • CSS之基础语法一文全解析
  • 大语言模型(LLM)按架构分类
  • 小黑黑日常积累大模型prompt句式2:【以段落的形式输出,不分点列举】【如果没有相关内容则不输出】【可读性强】【输出格式规范】
  • 音视频会议服务搭建(设计方案-Go服务端API业务逻辑流程图)-04
  • js随机生成一个颜色
  • Python 机器学习核心入门与实战进阶 Day 1 - 分类 vs 回归
  • 【Linux】Rocky Linux 安装 Jenkins
  • Jenkins 构建过程常见错误
  • serviceWorker缓存资源
  • 项目——视频共享系统测试
  • 多线程(进阶)
  • PTA N个数求和
  • 基于Hadoop的京东厨具商品数据分析及商品价格预测系统的设计与实现
  • GoFastDFS:轻量级高性能分布式文件存储解决方案(Linux安装部署)