Python3之协程爬虫应用

xiaoxiao2021-02-28  72

from gevent import monkey; monkey.patch_all() import gevent, requests def get_page(url): ret = requests.get(url).text with open('b.txt', 'a', encoding='utf-8') as f: f.write('%s-%s\n' % (url, len(ret))) # g1 = gevent.spawn(get_page, 'http://www.openstack.org') g2 = gevent.spawn(get_page, 'https://www.python.org') g3 = gevent.spawn(get_page, 'http://www.sina.com.cn/') gevent.joinall([g2, g3])

所用时间是网络延迟最长还有写文件最耗时的那个任务的时间!!!

转载请注明原文地址: https://www.6miu.com/read-46411.html

最新回复(0)