深入理解协程(四):async/await异步爬虫实战,,本文目录:同步方式爬
深入理解协程(四):async/await异步爬虫实战,,本文目录:同步方式爬
本文目录:
同步方式爬取博客标题async/await异步爬取博客标题本片为深入理解协程系列文章的补充。
你将会在从本文中了解到:async/await如何运用的实际的爬虫中。
案例
从CSDN上批量爬取指定文章的标题。文章列表如下:
urls = [ 'https://blog.csdn.net/Jmilk/article/details/103218919', 'https://blog.csdn.net/stven_king/article/details/103256724', 'https://blog.csdn.net/csdnnews/article/details/103154693', 'https://blog.csdn.net/dg_lee/article/details/103951021', 'https://blog.csdn.net/m0_37907797/article/details/103272967', 'https://blog.csdn.net/zzq900503/article/details/49618605', 'https://blog.csdn.net/weixin_44339238/article/details/103977138', 'https://blog.csdn.net/dengjin20104042056/article/details/103930275', 'https://blog.csdn.net/Mind_programmonkey/article/details/103940511', 'https://blog.csdn.net/xufive/article/details/102993570', 'https://blog.csdn.net/weixin_41010294/article/details/104009722', 'https://blog.csdn.net/yunqiinsight/article/details/103137022', 'https://blog.csdn.net/qq_44210563/article/details/102826406',]
同步爬虫
import requestsimport timefrom lxml import etreeurls = [ 'https://blog.csdn.net/Jmilk/article/details/103218919', 'https://blog.csdn.net/stven_king/article/details/103256724', ...此处略]def get_title(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36' } r = requests.get(url, headers) html = r.content title = etree.HTML(html).xpath('//h1[@class="title-article"]/text()')[0] print(title) def main(): for url in urls: get_title(url) if __name__ == '__main__': start = time.time() main() print(f'cost time: {time.time() - start}s')
输出结果如下:
4G LTE/EPC 协议栈Android-Universal-Image-Loader源码分析8年经验面试官详解 Java 面试秘诀AES中ECB模式的加密与解密(Python3.7)【图解算法面试】记一次面试:说说游戏中的敏感词过滤是如何实现的?java进阶(四)------java编程规范---代码质量检测工具FindBugs、PMD和CheckStyle的安装这是一份集合一线大厂Android工程师必备技能体系+学习路线!【程序人生】程序员接私活常用平台汇总你不得不了解的卷积神经网络发展史致 Python 初学者OOM别慌,手把手教你定位中国数据库OceanBase登顶之路网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))cost time: 6.065227508544922s
用时:6.065227508544922s。
async/await异步爬虫
要实现一个真正的异步爬虫,就需要引入aiohttp模块,aiohttp是一个利用asyncio的库,可以暂时看成协程版的requests。
import asyncioimport timeimport aiohttpfrom lxml import etreeurls = [ 'https://blog.csdn.net/Jmilk/article/details/103218919', 'https://blog.csdn.net/stven_king/article/details/103256724', ...此处略]async def async_get_url(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36' } async with aiohttp.ClientSession() as session: # 解释1 async with session.get(url, headers=headers) as r: html = await r.read() title = etree.HTML(html).xpath('//h1[@class="title-article"]/text()')[0] print(title) def async_main(): loop = asyncio.get_event_loop() tasks = [async_get_url(url) for url in urls] loop.run_until_complete(asyncio.wait(tasks)) loop.close() if __name__ == '__main__': start = time.time() async_main() print(f'cost time: {time.time() - start}s')
输出结果:
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))【程序人生】程序员接私活常用平台汇总致 Python 初学者中国数据库OceanBase登顶之路Android-Universal-Image-Loader源码分析OOM别慌,手把手教你定位这是一份集合一线大厂Android工程师必备技能体系+学习路线!AES中ECB模式的加密与解密(Python3.7)4G LTE/EPC 协议栈【图解算法面试】记一次面试:说说游戏中的敏感词过滤是如何实现的?8年经验面试官详解 Java 面试秘诀java进阶(四)------java编程规范---代码质量检测工具FindBugs、PMD和CheckStyle的安装你不得不了解的卷积神经网络发展史cost time: 0.6428999900817871s
说明:
解释1:此处为异步的上下文管理器,是aiohttp官方文档提供的写法。如果对上下文管理器不是很了解的话,可以参看【吃透Python上下文管理器】。
用时:0.6428999900817871s。从两种爬虫的输出结果中可以看到:
文章标题的顺序不同。同步爬虫会按照urls内部的url顺序依次爬取文章标题。而异步爬虫爬取的顺序并不完全和urls中的url顺序相同。爬取速度差异很大。异步爬虫速度大概是普通同步爬虫的8~10倍。异步爬虫充分利用了网络请求这段时间。从而提高了爬取效率。关于aiohttp的更多用法。会在后面文章讲到。
推荐阅读
深入理解协程(一):协程的引入
深入理解协程(二):yield from实现异步协程
深入理解协程(三):async/await实现异步协程
Python进阶:上下文管理器
关注公众号西加加先生一起玩转Python。
深入理解协程(四):async/await异步爬虫实战
相关内容
- 屏幕截图小工具的制作过程问题记录 python PIL pynput py
- 44.Python实现简易的图书管理系统,,首先展示一下图书管
- Selenium+python --定位下拉列表框并选取内容,,follow yoy
- Neovim中提示Error: Required vim compiled with +python,,Neovim在编
- Python小练(二),,# 1、输出99乘法
- Linux安装Python 3.8.1,,1、安装所需依赖[[
- python_面向对象编程,,初始面向对象一、类的
- 3种python调用其他脚本的方法,你还知道其他的方法吗?
- 分享《Python深度学习》+PDF+源码+弗朗索瓦·肖莱+张亮,
- Centos6.8下yum安装python2.7,,下载 ius-rel
评论关闭