pycharm 爬虫的数据存在哪了(利用PyCharm Profile分析异步爬虫效率详解)
类别:脚本大全 浏览量:2434
时间:2021-10-04 01:56:13 pycharm 爬虫的数据存在哪了
利用PyCharm Profile分析异步爬虫效率详解今天比较忙,水一下
下面的代码来源于这个视频里面提到的,github 的链接为:github.com/mikeckenned…
第一个代码如下,就是一个普通的 for 循环爬虫。原文地址。
|
import requests import bs4 from colorama import fore def main(): get_title_range() print ( "done." ) def get_html(episode_number: int ) - > str : print (fore.yellow + f "getting html for episode {episode_number}" , flush = true) url = f 'https://talkpython.fm/{episode_number}' resp = requests.get(url) resp.raise_for_status() return resp.text def get_title(html: str , episode_number: int ) - > str : print (fore.cyan + f "getting title for episode {episode_number}" , flush = true) soup = bs4.beautifulsoup(html, 'html.parser' ) header = soup.select_one( 'h1' ) if not header: return "missing" return header.text.strip() def get_title_range(): # please keep this range pretty small to not ddos my site. ;) for n in range ( 185 , 200 ): html = get_html(n) title = get_title(html, n) print (fore.white + f "title found: {title}" , flush = true) if __name__ = = '__main__' : main() |
这段代码跑完花了37s,然后我们用 pycharm 的 profiler 工具来具体看看哪些地方比较耗时间。
点击profile (文件名称)
之后获取到得到一个详细的函数调用关系、耗时图:
可以看到 get_html 这个方法占了96.7%的时间。这个程序的 io 耗时达到了97%,获取 html 的时候,这段时间内程序就在那死等着。如果我们能够让他不要在那儿傻傻地等待 io 完成,而是开始干些其他有意义的事,就能节省大量的时间。
稍微做一个计算,试用asyncio异步抓取,能将时间降低多少?
get_html这个方法耗时36.8s,一共调用了15次,说明实际上获取一个链接的 html 的时间为36.8s / 15 = 2.4s。**要是全异步的话,获取15个链接的时间还是2.4s。**然后加上get_title这个函数的耗时0.6s,所以我们估算,改进后的程序将可以用 3s 左右的时间完成,也就是性能能够提升13倍。
再看下改进后的代码。原文地址。
|
import asyncio from asyncio import abstracteventloop import aiohttp import requests import bs4 from colorama import fore def main(): # create loop loop = asyncio.get_event_loop() loop.run_until_complete(get_title_range(loop)) print ( "done." ) async def get_html(episode_number: int ) - > str : print (fore.yellow + f "getting html for episode {episode_number}" , flush = true) # make this async with aiohttp's clientsession url = f 'https://talkpython.fm/{episode_number}' # resp = await requests.get(url) # resp.raise_for_status() async with aiohttp.clientsession() as session: async with session.get(url) as resp: resp.raise_for_status() html = await resp.text() return html def get_title(html: str , episode_number: int ) - > str : print (fore.cyan + f "getting title for episode {episode_number}" , flush = true) soup = bs4.beautifulsoup(html, 'html.parser' ) header = soup.select_one( 'h1' ) if not header: return "missing" return header.text.strip() async def get_title_range(loop: abstracteventloop): # please keep this range pretty small to not ddos my site. ;) tasks = [] for n in range ( 190 , 200 ): tasks.append((loop.create_task(get_html(n)), n)) for task, n in tasks: html = await task title = get_title(html, n) print (fore.white + f "title found: {title}" , flush = true) if __name__ = = '__main__' : main() |
同样的步骤生成profile 图:
可见现在耗时为大约3.8s,基本符合我们的预期了。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。
原文链接:https://juejin.im/post/5cc05c005188250a912b2800
您可能感兴趣
- wx小程序请求封装(小程序wx.getUserProfile接口的具体使用)
- set statistics profile on的用法
- SQL Server Profile事件含义
- pycharm 爬虫的数据存在哪了(利用PyCharm Profile分析异步爬虫效率详解)
- 怎么查看mysql运行日志(通过Query Profiler查看MySQL语句运行时间的操作方法)
- MongoDB优化器profile
- windows性能计数器与SQL Server Profiler 组合分析性能
- Chrome谷歌浏览器开发者工具中Profiles的使用
- Sql Server profiler 分析器的理解
- 白蓝色穿搭(白蓝色衣服配什么裤子)
- 天空是什么颜色(天空是什么颜色的英语)
- 高马尾扎发(高马尾扎发教程视频)
- 这里输入关键词(请手动输入关键词)
- 小说 顾瑾岚拿出一套飞行棋,别说你连飞行棋都不会哦(顾瑾岚拿出一套飞行棋)
- 金品公司 界界乐中秋限定飞行棋礼盒 露营藤篮礼盒全新上市(界界乐中秋限定飞行棋礼盒)
热门推荐
- sqlcount函数转化为数值型(SQL判断是否"存在",还在用 count 操作?很耗时的!)
- mybatissql解析(mybatis动态sql实现逻辑代码详解)
- css3 图片样式处理(CSS3实现漂亮的照片墙效果的简单实例推荐)
- css3经典动画效果(CSS3 3D位移translate效果实例介绍)
- 面试时如何回答对工作的期望
- mysql server安装图解教程(mysql容器之间的replication配置实例详解)
- web服务器有几种(2019常见六种web服务器软件介绍)
- dedecms文章详情表(让Dedecms自带搜索实现全文检索支持标题与内容)
- 腾讯云开启所有端口(腾讯云端口怎么设置?腾讯云CVM开启端口图文教程)
- Mysql中CONCAT、CONCAT_WS字符串拼接函数
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9