我正在使用Flask,Gevent和scrapy进行项目。基本思想是输入一个url并以输入作为参数启动一个爬虫程序。它目前似乎与通过websocket传输的输出效果很好。
我很好奇处理多个抓取器同时运行的最佳方法是什么,所以如果两个人同时输入一个url。我认为最好的方法是使用队列系统,理想情况下我只希望同时运行可控数量的爬虫。
有没有关于如何使用我已经使用的库进行此操作的建议?或者可能建议采用不同的方法?
答案 0 :(得分:0)
尝试使用nodejs,webtcp(对于websockets)和每个爬虫的异步调用。一旦完成爬网,您可以将其保存在临时存储中,例如memcached或带有过期密钥的redis。
所以当有类似的抓取请求时,您可以从临时存储中提供
答案 1 :(得分:0)
如果抓取工具是gevent作业,则可以使用池。
http://www.gevent.org/gevent.pool.html
Group的子类提供了一种限制并发的方法:它的spawn方法会阻塞池中greenlet的数量是否已达到限制,直到有空闲插槽。
伪代码:
crawler_pool = Pool(10)
def spawncrawler(url):
def start():
crawler_pool.spawn(crawl, url) # blocks when max is reached.
gevent.spawn(start)
# give a response to the browser. this will always succeed because
# i put the spawning of the crawler in a separate greenlet so if max
# 10 crawlers is reached the greenlet just holds on untill there is space
# and client can get default response..