首页 > 编程语言 > 详细

Python 爬虫

时间:2017-10-29 17:05:56      阅读:278      评论:0      收藏:0      [点我收藏+]
from urllib import request
import gevent,time
from gevent import monkey
monkey.patch_all()

def f(url):
print(‘GET: %s‘ % url)
resp = request.urlopen(url)
data = resp.read()
print(‘%d bytes received from %s.‘ % (len(data), url))

urls = [‘https://www.python.org/‘,
‘https://www.yahoo.com/‘,
‘https://www.sohu.com/‘ ]
time_start = time.time()
for url in urls:
f(url)
print("同步cost",time.time() - time_start)
async_time_start = time.time()
gevent.joinall([
gevent.spawn(f, ‘https://www.python.org/‘),
gevent.spawn(f, ‘https://www.yahoo.com/‘),
gevent.spawn(f, ‘https://github.com/‘),
])
print("异步cost",time.time() - async_time_start)

Python 爬虫

原文:http://www.cnblogs.com/xiesongyou/p/7750344.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!