
# 一个简单的小爬虫,将3个页面的数据保存到data.html,对比协程和非协程的使用时间
"""非协程
1、通过urlopen获取数据
2、写入文件
3、使用三个页面,通过for循环执行(非协程会在IO阻塞处等待),用时较长
"""
from urllib.request import urlopen
import time
def foo(url):
print('GET:{}'.format(url))
resp = urlopen(url)
data = resp.read()
with open('data.html', 'wb') as f:
f.write(data)
print('{} bytes received from {}'.format(len(data), url))
url_list = ['https://www.python.org/', 'https://github.com/', 'http://www.yahoo.com/']
start_time = time.time()
for url in url_list:
foo(url)
end_time = time.time() - start_time
print(end_time)原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。