码迷,mamicode.com
首页 > 编程语言 > 详细

Python 爬虫

时间:2017-10-29 17:44:13      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:urllib   def   get   time()   www   async   www.   received   url   

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 爬虫

标签:urllib   def   get   time()   www   async   www.   received   url   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!