Scrapy立即运行所有蜘蛛。我想一次只跑一只蜘蛛。抓取抓取<蜘蛛>

时间:2019-10-26 18:55:39

标签: python-3.x web-scraping scrapy

我是Scrapy的新手,正在尝试使用该框架。真正令人沮丧的是,当我运行“ scrapy crawl(蜘蛛的名称)”时,它会运行“ spiders”文件夹中的每个蜘蛛。因此,我要么必须等待所有蜘蛛运行,要么要注释掉除我正在使用的所有蜘蛛之外的所有蜘蛛。这很烦人。我该如何做才能使刮板一次只能跑一只蜘蛛?

2 个答案:

答案 0 :(得分:2)

您可以通过脚本(https://scrapy.readthedocs.io/en/latest/topics/practices.html#run-from-script)运行scrapy, 例如:

import scrapy
from scrapy.crawler import CrawlerProcess

class YourSpider(scrapy.Spider):
    # Your spider definition


process = CrawlerProcess()
process.crawl(YourSpider)
process.start() 

答案 1 :(得分:0)

它不应该运行整个蜘蛛,尽管它确实可以编译并运行一些东西,因为这就是它拉蜘蛛名称的方式(我认为还有其他原因,否则这似乎是一种奇怪的设置方式)。如果您发布蜘蛛,我们可以查看运行情况与否。

我遇到了同样的问题,因为我的蜘蛛修改了csv文件,包括重命名/删除它们,这在我只想运行特定蜘蛛的时候搞砸了。我的解决方案是让蜘蛛仅在它们实际运行或关闭时才执行某些任务。这里的文档:https://docs.scrapy.org/en/latest/topics/signals.html,但我发现缺少它。 这是我使用的代码。除了更改蜘蛛名称之外,from_crawler部分可以单独放置。将您想要的任何内容放入spider_closed部分

@classmethod
def from_crawler(cls, crawler, *args, **kwargs):
    spider = super(SixPMSpider, cls).from_crawler(crawler, *args, **kwargs)
    crawler.signals.connect(spider.spider_closed, signal=signals.spider_closed)
    return spider

def spider_closed(self, spider):
    os.remove(self.name+'_price_list.csv')
    os.rename(self.name+'_price_list2.csv', self.name+'_price_list.csv')