aiohttp 的安装 | 珊瑚贝

之前我们介绍的 requests 库是一个阻塞式 HTTP 请求库,当我们发出一个请求后,程序会一直等待服务器的响应,直到得到响应后程序才会进行下一步的处理,其实这个过程是比较耗费资源的。如果程序可以在这个等待过程中做一些其他的事情,如进行请求的调度、响应的处理等等,那么爬取效率一定会大大提高。

Aiohttp 就是这样一个提供异步 Web 服务的库,从 Python3.5 版本开始,Python 中加入了 async/await 关键字,使得回调的写法更加直观和人性化,Aiohttp 的异步操作借助于 async/await 关键字写法变得更加简洁,架构更加清晰。使用异步请求库来进行数据抓取会大大提高效率,下面我们来看一下这个库的安装方法。

相关链接

pip 安装

推荐使用 pip 安装,命令如下:

1
pip3 install aiohttp

另外官方还推荐安装如下两个库,一个是字符编码检测库 cchardet,另一个是加速 DNS 解析库 aiodns,安装命令如下:

1
pip3 install cchardet aiodns

测试安装

安装完成之后,可以在 Python 命令行下测试。

1
2
$ python3
>>> import aiohttp

如果没有错误报出,则证明库已经安装好了。

来源:https://cuiqingcai.com/31056.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_11640.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?