[Python3 网络爬虫开发实战] 1.2.6-aiohttp 的安装 | 珊瑚贝

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

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

1. 相关链接

2. pip 安装

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

1
pip3 install aiohttp

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

1
pip3 install cchardet aiodns

3. 测试安装

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

1
2
$ python3
>>> import aiohttp

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

4. 结语

我们会在后面的实例中用到这个库,比如维护一个代理池时,利用异步方式检测大量代理的运行状况,会极大地提升效率。

来源:https://cuiqingcai.com/5163.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_11714.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?