Python协程并发 爬虫下载
来源:互联网 发布:朗文高阶英语字典 mac 编辑:程序博客网 时间:2024/05/18 00:24
我用的是gevent模块来使用协程,另外这里强烈推荐下Anaconda3,因为几乎所有包都有,安装很方便。
gevent并发下载我是借鉴了http://blog.csdn.net/wzm112/article/details/8171146这一篇文章的,不过因为它有点旧了,所有我用了requests库重新写了下。
代码非常简单,速度却比多进程和多线程快了非常多。
代码如下:
#同样的,下载30个url
import random
import time
import gevent
import gevent.monkey
gevent.monkey.patch_all()#有IO时使用
import requests
def download(url):
a=random.randint(0,40)
s=requests.Session()
res=s.get(url)
with open('%d.jpg'%a,'wb')as f:
f.write(res.content)
if __name__=='__main__':
s=time.time()
f=open('E:\Python\py\process+xiec\\test.txt','r')
b=f.readlines()
f.close()
for i in b:
ge=[gevent.spawn(download,i)]
gevent.joinall(ge)
e=time.time()
print(e-s)
输出时间:1.499
比多进程(8s多)和多线程(4s多)快多了
另外,我也试了多进程+协程(听说并发更快)的方式来并发下载,试了3次,电脑都崩溃了。
这方式我在想想吧。
- Python协程并发 爬虫下载
- python爬虫-->并发下载
- 【Python爬虫4】并发并行下载
- Python爬虫下载文档
- python爬虫-->下载缓存
- python 爬虫下载图片
- Python 网络爬虫-下载图片
- [python][爬虫]暴漫gif下载
- [python][爬虫]暴漫gif下载
- python 爬虫下载网站图片
- python 爬虫批量下载图片
- python 简单爬虫下载图片
- python爬虫学习--下载图片
- Python爬虫之下载媒体文件
- python网络爬虫(五):并发抓取
- Python学习之多进程并发爬虫
- python爬虫(python下载20160804)
- WSWP(用 python写爬虫) 笔记五:并发爬虫
- 微信 SQLite 数据库修复实践
- js里的响应事件
- 微信公众号支付支付验证签名失败及公众号支付总结
- hbuilder打包APP
- Python count()
- Python协程并发 爬虫下载
- Spring Security于Spring Boot结合的认证授权机制流程(一)
- twig渲染html字符串(备忘)
- Android打包的那些事
- SB
- leetcode:Count Numbers with Unique Digits
- Oracle知识点连载(四)
- Android学习之---双击back退出app
- php时间戳在前端用js转换为时间格式