我想让一个抓取爬虫在芹菜任务工人内部不断运行,可能使用something like this。或者,正如in the docs所建议的,这个想法是使用爬虫程序来查询返回XML响应的外部API。我希望将我想要查询的URL (或查询参数并让crawler构建URL)传递给crawler,crawler将进行URL调用,并将提取的项返回给我。一旦爬虫程序开始运行,我如何将我想要获取的这个新URL传递给爬虫程序。我不想在每次想要给爬虫一个新的URL时都重新启动爬虫,而是希望爬虫无所事事地等待URL爬行。
我发现的在另一个python进程中运行scrapy的两个方法使用一个新进程来运行爬虫程序。我不希望每次我想要抓取一个URL时,都必须派生和拆卸一个新的进程,因为这是相当昂贵和不必要的。
发布于 2013-05-23 10:53:47
只要有一个可以轮询数据库(或文件?)的爬行器当提供一个新的URL时,它会为它创建并产生一个新的Request()对象。
您可以很容易地手动构建它。可能有一种比这更好的方法,但这基本上就是我为开放代理刮板所做的。爬行器从数据库中获取所有“潜在”代理的列表,并为每个代理生成一个Request()对象-当它们被返回时,它们将沿着链向下分派,并由下游中间件进行验证,它们的记录通过项管道进行更新。
发布于 2013-05-24 21:40:39
您可以使用消息队列(如IronMQ--完全公开,我在开发人员布道者的IronMQ公司工作)来传递URL。
然后在crawler中,轮询队列中的URL,并根据检索到的消息进行搜索。
您链接到的示例可以更新(这是未经测试的伪代码,但您应该了解基本概念):
from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from scrapy import log
from testspiders.spiders.followall import FollowAllSpider
from iron-mq import IronMQ
mq = IronMQ()
q = mq.queue("scrape_queue")
crawler = Crawler(Settings())
crawler.configure()
while True: # poll forever
msg = q.get(timeout=120) # get messages from queue
# timeout is the number of seconds the message will be reserved for, making sure no other crawlers get that message. Set it to a safe value (the max amount of time it will take you to crawl a page)
if len(msg["messages"]) < 1: # if there are no messages waiting to be crawled
time.sleep(1) # wait one second
continue # try again
spider = FollowAllSpider(domain=msg["messages"][0]["body"]) # crawl the domain in the message
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run() # the script will block here
q.delete(msg["messages"][0]["id"]) # when you're done with the message, delete ithttps://stackoverflow.com/questions/16704753
复制相似问题