基于协程的异步爬虫
来源:互联网 发布:怎么免费做网络推广 编辑:程序博客网 时间:2024/06/10 19:49
基于tornado框架的异步爬虫小例子:
#!/usr/bin/env python# -*- coding: utf-8 -*-# created by fhqplzj on 2017/07/19 下午5:48import loggingimport timefrom datetime import timedeltafrom urlparse import urljoin, urldefragfrom scrapy import Selectorfrom tornado.gen import coroutine, Returnfrom tornado.httpclient import AsyncHTTPClientfrom tornado.ioloop import IOLoopfrom tornado.queues import Queuelogging.basicConfig()base_url = 'http://www.tornadoweb.org/en/stable/'concurrency = 10@coroutinedef get_links_from_url(url): try: response = yield AsyncHTTPClient().fetch(url) print 'fetched %s' % url html = response.body if isinstance(response.body, str) else response.body.decode() urls = [urljoin(url, urldefrag(new_url)[0]) for new_url in get_links(html)] except Exception as e: print 'Exception: %s %s' % (e, url) raise Return([]) raise Return(urls)def get_links(html): return Selector(text=html).xpath('//a/@href').extract()@coroutinedef main(): q = Queue() start = time.time() # fetching: 已经抓的和正在抓的 # fetched: 已经抓的 fetching, fetched = set(), set() @coroutine def fetch_url(): current_url = yield q.get() try: if current_url in fetching: return print 'fetching %s' % current_url fetching.add(current_url) urls = yield get_links_from_url(current_url) fetched.add(current_url) for new_url in urls: if new_url.startswith(base_url): yield q.put(new_url) finally: q.task_done() @coroutine def worker(): while True: yield fetch_url() q.put(base_url) for _ in range(concurrency): worker() yield q.join(timeout=timedelta(seconds=300)) assert fetching == fetched print 'Done in %d seconds, fetched %s URLs.' % (time.time() - start, len(fetched))if __name__ == '__main__': IOLoop.current().run_sync(main)
阅读全文
0 0
- 基于协程的异步爬虫
- Python实现基于协程的异步爬虫(一)
- 异步爬虫框架与协程浅析
- 基于python的爬虫
- 异步爬虫
- 基于WebKit的网络爬虫
- 基于webkit技术的爬虫
- 基于WebKit的网络爬虫
- 基于scrapy的小爬虫
- 基于java的分布式爬虫
- 基于测试的爬虫selenium
- 基于python的pixiv爬虫
- 基于python3的网络爬虫
- 基于java的分布式爬虫
- 基于Mojo的perl爬虫
- 基于node的微小爬虫
- 基于scrapy的简单爬虫
- 基于Scrapyd的爬虫部署
- vscode 切换编码
- 日志增强解析处理
- 模板方法之无法解析的外部符号
- PowerDesigner基础应用
- Java中级知识点
- 基于协程的异步爬虫
- POJ 1321 深搜 棋盘问题
- LeetCode 513 Find Bottom Left Tree Value(二叉树层序遍历)
- [BZOJ1514]添加括号
- [LeetCode] 122. Best Time to Buy and Sell Stock II
- spring读取配置文件的两种方式
- Android应用如何匹配不同的手机分辨率
- Socket小练习
- 管闲事的小明