【问题标题】:Scrapy get notification when all spiders are closed当所有蜘蛛关闭时,Scrapy 会收到通知
【发布时间】:2020-03-25 20:50:15
【问题描述】:

我正在使用 django 来开始爬虫

scrapyd = ScrapydAPI('http://localhost:6800')
spiders = scrapyd.list_spiders("default")
for spider in spiders:
        scrapyd.schedule("default", spider, list_id=list.id, spiders_number=3)

我是否能够根据list_id 连接与单个请求相关的所有蜘蛛。我希望在所有蜘蛛完成工作时获得信息。将这些信息从蜘蛛发送到 django 很简单(我可以获取 django 模型的实例并对其进行更新)。但我不知道当前蜘蛛是否是最后一个正在运行的蜘蛛。

蜘蛛之间可以交换数据吗?有没有众所周知的方法可以做到这一点?

【问题讨论】:

    标签: python django scrapy


    【解决方案1】:

    scrapy 管道中有一个名为 close_spider 的事件方法,它在蜘蛛完成并即将关闭之前被调用。

    https://docs.scrapy.org/en/latest/topics/item-pipeline.html

    但是,由于您正在遍历所有蜘蛛,所以它是无用的。 我能想到的唯一解决方案是在最后一个循环中调用通知。

    【讨论】:

      【解决方案2】:

      Scrapyd-API 不会同时运行多个蜘蛛(作业),因此从终端中的scrapyd.list_spiders("default") 获取列表,列表中的最后一个蜘蛛将是最后一个运行的蜘蛛。 然后只需在那个蜘蛛中添加

      def close(self):
          #send notification()
      

      【讨论】:

        猜你喜欢
        • 2012-11-20
        • 2017-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-21
        相关资源
        最近更新 更多