强大的异步爬虫 with aiohttp

  • 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kun1280437633/article/details/80685334
看到现在网络上大多讲的都是requests、scrapy,却没有说到爬虫中的神器: aiohttp

aiohttp 介绍

aiohttp是什么,官网上有这样一句话介绍:Async HTTP client/server for asyncio and Python,翻译过来就是 asyncio和Python的异步HTTP客户端/服务器

主要特点是:

  1. 支持客户端和HTTP服务器。

  2. 无需使用Callback Hell即可支持Server WebSockets和Client WebSockets。

  3. Web服务器具有中间件,信号和可插拔路由。

emmmm,好吧,还是来看代码吧

Client example:

 

import aiohttp
import asyncio

async def fetch(session, url):
   async with session.get(url) as response:
       return await response.text()

async def main():
   async with aiohttp.ClientSession() as session:
       html = await fetch(session, 'http://httpbin.org/headers')
       print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

output:

 

{ "headers":{ "Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close",

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值