python并发利器tomorrow
tomorrow是我最近在用的一个爬虫利器,该模块属于第三方的模块,使用起来非常的方便,只需要用其中的threads方法作为装饰器去修饰一个普通的函数,既可以达到并发的效果,本篇将用实例来展示tomorrow的强大之处。后面文章将对tomorrow的实现原理做进一步的分析。
1.安装第三方包
pip install requests_html #网络请求包
pip install fake-useragent #获取useragent包
pip install tomorrow
2.普通下载方式
在这里我们用20个电影网址进行测试,并获取其标题,计算所用的时间
start=time.time()
for i in url_list:
print(get_xpath(get_req(i),"//title//text()"))
end=time.time()
print("普通方式花费时间",end-start)
get_req是我定义的访问网络的一个方法,get_xpath是为例使用xpath表达式获取其结果,这里是获取网址的标题。20个电影网址普通方式访问的结果在8-9秒之间。
3使用tomorrow以后
start2 = time.time()
req_list = []
for url in url_list:
req = async_get_req(url)
req_list.append(req) for req in req_list:
print(get_xpath(req, "//title//text()"))
end2 = time.time()
print("并发后花费时间", end2 - start2)
如果我们想要使用tomorrow,就要尽量减少耗时操作,访问网络并等待其回应就是一个非常耗时的工作,在这里我们需要做的是,并发的时候除了访问网络不要做其他操作,然后我们把获取的请求存一个列表,然后再去循环做其他操作,看不懂我说的没关系,直接看下面代码并尝试几次就明白了。用时为2s-3s
4.测试结果对比 来看完整代码
import time
from requests_html import HTMLSession
from fake_useragent import UserAgent as ua
from tomorrow import threads headers = {"User-Agent": ua().Chrome}
session = HTMLSession()
url_list = ["https://movie.douban.com",
"http://www.1905.com/",
"http://www.mtime.com/",
"https://www.dy2018.com/",
"http://dytt8.net",
"https://www.piaohua.com/",
"http://maoyan.com",
"https://www.xigua110.com/",
"https://www.vmovier.com/",
"http://movie.kankan.com/",
"https://107cine.com/",
"http://movie.youku.com",
"http://film.qq.com","https://dianying.taobao.com/",
"http://www.wandafilm.com/",
"http://www.dygang.net/","http://dianying.2345.com/",
] def get_req(url, timeout=10):
req = session.get(url, headers=headers, timeout=timeout)
if req.status_code == 200:
return req @threads(5)
def async_get_req(url, timeout=10):
req = session.get(url, headers=headers, timeout=timeout)
if req.status_code == 200:
return req def get_xpath(req, xpath_str):
return req.html.xpath(xpath_str)[0].strip().replace("\n", "") start=time.time()
for i in url_list:
print(get_xpath(get_req(i),"//title//text()"))
end=time.time()
print("普通方式花费时间",end-start) start2 = time.time()
req_list = []
for url in url_list:
req = async_get_req(url)
req_list.append(req) for req in req_list:
print(get_xpath(req, "//title//text()"))
end2 = time.time()
print("并发后花费时间", end2 - start2)
运行三次上面的程序记录下每次的结果
第一次:
普通方式花费时间 7.883908271789551
并发后花费时间 2.2888755798339844
第二次:
普通方式花费时间 8.522203207015991
并发后花费时间 2.4674007892608643
第三次:
普通方式花费时间 9.062756061553955
并发后花费时间 2.8703203201293945
tomorrow使用起来很简单,在普通的函数上面加个threads装饰器即可以实现并发效果, 括号中的数字是表示并发的次数,经过我的测试并不是并发次数越多越好,你需要选择一个中间点,因为还会受到网速的影响,我觉得一般并发数5-10就好.
转载自:https://www.cnblogs.com/c-x-a/p/9572326.html
python并发利器tomorrow的更多相关文章
- Sublime Text配置Python开发利器
Sublime Text配置Python开发利器 收好了 自动提示 jedi 代码格式化 Python PEP8 autoformat 如果还需要在shell中搞搞研究的话,ipython将是很好的选 ...
- python 开发利器
UliPad 初体验----python 开发利器 Posted on 2013-10-28 22:36 虫师 阅读(436) 评论(3) 编辑 收藏 学习python 有段时间,最近博客更新比较慢了 ...
- python爬虫利器Selenium使用详解
简介: 用pyhon爬取动态页面时普通的urllib2无法实现,例如下面的京东首页,随着滚动条的下拉会加载新的内容,而urllib2就无法抓取这些内容,此时就需要今天的主角selenium. Sele ...
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- Python并发编程__多进程
Python并发编程_多进程 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大 ...
- Python并发编程的几篇文章
Python几种并发实现方案的性能比较 http://www.elias.cn/Python/PyConcurrency?from=Develop.PyConcurrency python并发编程 h ...
- Python并发编程之深入理解yield from语法(八)
大家好,并发编程 进入第八篇. 直到上一篇,我们终于迎来了Python并发编程中,最高级.最重要.当然也是最难的知识点--协程. 当你看到这一篇的时候,请确保你对生成器的知识,有一定的了解.当然不了解 ...
- Python并发目录
Python并发目录 Python-socket网络编程 Python网络编程-IO阻塞与非阻塞及多路复用 Python进程-理论 Python进程-实现 Python进程间通信 Python进程池 ...
- Python监控服务器利器--psutil
Python监控服务器利器--psutil 服务器的监控通过安装一些常用的监控软件之外,有时也需要运行一些shell或Python脚本:shell下可以使用系统自带的ps/free/top/df等sh ...
随机推荐
- map最最最基本用法
map<a,b>c中,a,b是变量类型 参数定义的map的名字 #include<stdio.h> #include<map> //头文件 map<int,c ...
- Monorepo All In One
Monorepo All In One monorepos 只是一种思想,或设计模式,架构风格 https://trunkbaseddevelopment.com/monorepos/ Lerna h ...
- vue 二级子路由跳转不了 bug
vue 二级子路由跳转不了 bug @click.prevent 阻止原生事件的冒泡 <li class="tools-hover-box-list-item" v-for= ...
- Fullscreen API All In One
Fullscreen API All In One 全屏显示 https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API https ...
- ADN vs CDN All In One
ADN vs CDN All In One Netlify & JAMstack https://app.netlify.com/teams/xgqfrms/sites ADN Applica ...
- LeetCode 二叉树,两个子节点的最近的公共父节点
LeetCode 二叉树,两个子节点的最近的公共父节点 二叉树 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共父亲节点 https://leetcod ...
- Express vs Koa
Express vs Koa https://www.esparkinfo.com/express-vs-koa.html https://www.cleveroad.com/blog/the-bes ...
- yarn & uninstall global & yarn global remove
yarn uninstall global yarn global remove https://yarnpkg.com/lang/en/docs/cli/remove/ https://yarnpk ...
- js replace all
js replace all https://stackoverflow.com/questions/1144783/how-can-i-replace-all-occurrences-of-a-st ...
- Chrome 80 & SameSite & cookie
Chrome 80 & SameSite & cookie chrome://settings/help https://developers.google.com/web/updat ...