第102天: Python异步之aiohttp

什么是 aiohttp?一个异步的 HTTP 客户端\服务端框架,基于 asyncio 的异步模块。可用于实现异步爬虫,更快于 requests 的同步爬虫。

安装

pip install aiohttp

aiohttp 和 requests

requests 版爬虫

requests 同步方式连续 30 次简单爬取 http://httpbin.org 网站

import requestsfrom datetime import datetime

def fetch(url): r = requests.get(url) print(r.text)
start = datetime.now()
for i in range(30): fetch('http://httpbin.org/get')
end = datetime.now()
print("requests版爬虫花费时间为:")print(end - start)

示例结果

# 打印网站返回的内容....requests版爬虫花费时间为:0:00:43.248761

从爬取结果可以看出,同步爬取 30 次网站将花费 43 秒左右的时间,耗时非常长。

aiohttp 版爬虫

使用 aiohttp 和 asyncio 异步方式简单爬取 30 次网站

import aiohttpimport asynciofrom datetime import datetime

async def fetch(client): async with client.get('http://httpbin.org/get') as resp: assert resp.status == 200 return await resp.text()

async def main(): async with aiohttp.ClientSession() as client: html = await fetch(client) print(html)
loop = asyncio.get_event_loop()
tasks = []for i in range(30): task = loop.create_task(main()) tasks.append(task)
start = datetime.now()
loop.run_until_complete(main())
end = datetime.now()
print("aiohttp版爬虫花费时间为:")print(end - start)

示例结果

# 打印网站返回的内容....aiohttp版爬虫花费时间为:0:00:00.539416

从爬取时间可以看出,aiohttp 异步爬取网站只用了 0.5 秒左右的时间,比 requests 同步方式快了 80 倍左右,速度非常之快。

同一个 session

aiohttp.ClientSession() 中封装了一个 session 的连接池,并且在默认情况下支持 keepalives,官方建议在程序中使用单个 ClientSession 对象,而不是像上面示例中的那样每次连接都创建一个 ClientSession 对象,除非在程序中遇到大量的不同的服务。

将上面的示例修改为:

import aiohttpimport asynciofrom datetime import datetime

async def fetch(client): print("打印 ClientSession 对象") print(client) async with client.get('http://httpbin.org/get') as resp: assert resp.status == 200 return await resp.text()

async def main(): async with aiohttp.ClientSession() as client: tasks = [] for i in range(30): tasks.append(asyncio.create_task(fetch(client))) await asyncio.wait(tasks)
loop = asyncio.get_event_loop()
start = datetime.now()
loop.run_until_complete(main())
end = datetime.now()print("aiohttp版爬虫花费时间为:")print(end - start)

示例结果

# 重复30遍打印 ClientSession 对象<aiohttp.client.ClientSession object at 0x1094aff98>aiohttp版爬虫花费时间为:0:00:01.778045

从上面爬取的时间可以看出单个 ClientSession 对象比多个 ClientSession 对象多花了 3 倍时间。ClientSession 对象一直是同一个 0x1094aff98。

返回值

Json 串

在上面的示例中使用 response.text() 函数返回爬取到的内容,aiohttp 在处理 Json 返回值的时候,可以直接将字符串转换为 Json。

async def fetch(client): async with client.get('http://httpbin.org/get') as resp: return await resp.json()

示例结果

{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'Python/3.7 aiohttp/3.6.2'}, 'origin': '49.80.42.33, 49.80.42.33', 'url': 'https://httpbin.org/get'}

当返回的 Json 串不是一个标准的 Json 时,resp.json() 函数可以传递一个函数对 json 进行预处理,如:resp.json(replace(a, b)),replace()函数表示 a 替换为 b。

字节流

aiohttp 使用 response.read() 函数处理字节流,使用 with open() 方式保存文件或者图片

async def fetch(client): async with client.get('http://httpbin.org/image/png') as resp: return await resp.read()

async def main(): async with aiohttp.ClientSession() as client: image = await fetch(client) with open("/Users/xxx/Desktop/image.png", 'wb') as f: f.write(image)

response.read() 函数可以传递数字参数用于读取多少个字节,如:response.read(3)读取前 3 个字节。

参数

aiohttp 可以使用 3 种方式在 URL 地址中传递参数

async def fetch(client): params = [('a', 1), ('b', 2)] async with client.get('http://httpbin.org/get',params=params) as resp: return await resp.text()

示例 URL 地址

http://httpbin.org/get?a=1&b=2
async def fetch(client): params = {"a": 1,"b": 2} async with client.get('http://httpbin.org/get',params=params) as resp: return await resp.text()

示例 URL 地址

http://httpbin.org/get?a=1&b=2
async def fetch(client): async with client.get('http://httpbin.org/get',params='q=aiohttp+python&a=1') as resp: return await resp.text()

示例 URL 地址

http://httpbin.org/get?q=aiohttp+python&a=1

请求头

aiohttp 在自定义请求头时,类似于向 URL 传递参数的方式

async def fetch(client): headers = {'content-type': 'application/json', 'User-Agent': 'Python/3.7 aiohttp/3.7.2'} async with client.get('http://httpbin.org/get',headers=headers) as resp: return await resp.text()

COOKIES

cookies 是整个会话共用的,所以应该在初始化 ClientSession 对象时传递

async def fetch(client): async with client.get('http://httpbin.org/get') as resp: return await resp.text()

async def main(): cookies = {'cookies': 'this is cookies'} async with aiohttp.ClientSession(cookies=cookies) as client: html = await fetch(client) print(html)

POST 方式

在前面的示例中都是以 GET 方式提交请求,下面用 POST 方式请求

async def fetch(client): data = {'a': '1', 'b': '2'} async with client.post('http://httpbin.org/post', data = data) as resp: return await resp.text()

示例结果

{ "args": {}, "data": "", "files": {}, "form": { "a": "1", "b": "2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "7", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "Python/3.7 aiohttp/3.6.2" }, "json": null, "origin": "49.80.42.33, 49.80.42.33", "url": "https://httpbin.org/post"}
aiohttp版爬虫花费时间为:0:00:00.514402

在示例结果中可以看到 form 中的内容就是模拟 POST 方式提交的内容

超时

在请求网站时,有时会遇到超时问题,aiohttp 中使用 timeout 参数设置,单位为秒数,aiohttp 默认超时时间为 5 分钟

async def fetch(client): async with client.get('http://httpbin.org/get', timeout=60) as resp: return await resp.text()

总结

aiohttp 以异步的方式爬取网站耗时远小于 requests 同步方式,这里列举了一些 aiohttp 常用功能,希望对大家有所帮助。

代码地址

示例代码:https://github.com/JustDoPython/python-100-day/tree/master/day-102

系列文章
第101天:Python asyncio
从 0 学习 Python 0 - 100 大合集总结
(0)

相关推荐

  • (25条消息) 在Vue中使用async函数

    在Vue中使用async函数 async/await语法 在生命周期钩子上使用async函数 在methods中使用async函数 源代码 async/await语法 在ES7标准中新增了async和 ...

  • 爬虫爬取代理ip

    import urllib.request from bs4 import BeautifulSoup import re import time import random # ---------- ...

  • awesome asyncio-精选python异步框架清单集合

    Python 3.4引入标准库的Python asyncio模块提供了使用协程编写单线程并发代码,通过套接字和其他资源对I / O进行多路访问,运行网络客户端和服务器以及其他相关原语的基础结构. As ...

  • 如何使用Python异步编程进行API调用 | 区块链研究实验室

    原创 链三丰 区块链研究实验室 今天 收录于话题 #Python1 #区块链技术33 #区块链44 #API1 #区块链应用30 本文中,将向大家介绍如何使用Python异步编程,以便您可以更快地进行 ...

  • ASP.NET Core Blazor Webassembly 之 渐进式应用(PWA)

    Blazor支持渐进式应用开发也就是PWA.使用PWA模式可以使得web应用有原生应用般的体验. 什么是PWA PWA应用是指那些使用指定技术和标准模式来开发的web应用,这将同时赋予它们web应用和 ...

  • eventlet-具有WSGI支持的python异步框架

    Eventlet 是 Python 的并发网络库,它允许您更改运行代码的方式,而不是编写代码的方式. 它使用 epoll 或 kqueue 或 libevent 来实现高度可扩展的非阻塞 I/O. 协 ...

  • Python异步爬虫详解

    一.同步与异步 异步编程可以大幅度的提高系统的吞吐量,提高单位时间内发出的请求数目.之前大邓写的爬虫都是同步,就是对aurl发起请求,等待响应.然后再访问burl,等待响应... 大量的时间消耗在等待 ...

  • trio-好用的python异步并发和IO库

    Trio是好用的Python异步并发和I/O库. Trio项目的目标是为Python生成一个用于生产环境的高质量的异步并发本地的I/O库.像所有异步库一样,它的主要目的是帮助您编写使用并行化I/O同时 ...

  • 【Python爬虫】:使用高性能异步多进程爬虫获取豆瓣电影Top250

    在本篇博文当中,将会教会大家如何使用高性能爬虫,快速爬取并解析页面当中的信息.一般情况下,如果我们请求网页的次数太多,每次都要发出一次请求,进行串行执行的话,那么请求将会占用我们大量的时间,这样得不偿 ...

  • python接口自动化37-模拟ajax异步请求(X-Requested-With:XMLHttpRequest)

    前言 有些接口请求头部带上X-Requested-With:XMLHttpRequest ,返回数据是 json .如果头部不加这个参数,返回数据是普通 html 文本. 这种头部带上X-Reques ...

  • 异步 Python 比同步 Python 快在哪里?

    Python那些事 1周前 原文链接: https://blog.miguelgrinberg.com/post/sync-vs-async-python-what-is-the-difference ...

  • python使用Flask,Redis和Celery的异步任务

    原文链接:http://tecdat.cn/?p=8336 介绍 随着Web应用程序的发展和使用的增加,用例也变得多样化.我们现在正在建设和使用网站来执行比以往任何时候都更复杂的任务.其中一些任务可以 ...