【问题标题】:Scrapy Infinite loop with CrawlerProcess使用 CrawlerProcess 的 Scrapy 无限循环
【发布时间】:2021-05-09 18:12:07
【问题描述】:

我目前正在运行 Scrapy v2.5,我想运行无限循环。我的代码:

class main():

    def bucle(self, array_spyder, process):
        mongo       = mongodb(setting)
        for spider_name in array_spider:
            process_init.crawl(spider_name, params={ "mongo": mongo, "spider_name": spider_name})
        process.start()
        process.stop()
        mongo.close_mongo()

if __name__ == "__main__":
    setting     = get_project_settings()
    while True:
        process = CrawlerProcess(setting)
        array_spider = process.spider_loader.list()
        class_main = main()
        class_main.bucle(array_spider, process)

但这导致错误消息如下:

Traceback (most recent call last):
  File "run_scrapy.py", line 92, in <module>
    process.start()
  File "/usr/local/lib/python3.8/dist-packages/scrapy/crawler.py", line 327, in start
    reactor.run(installSignalHandlers=False)  # blocking call
  File "/usr/local/lib/python3.8/dist-packages/twisted/internet/base.py", line 1422, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)
  File "/usr/local/lib/python3.8/dist-packages/twisted/internet/base.py", line 1404, in startRunning
    ReactorBase.startRunning(cast(ReactorBase, self))
  File "/usr/local/lib/python3.8/dist-packages/twisted/internet/base.py", line 843, in startRunning
    raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable

谁能帮帮我??

【问题讨论】:

  • 如果您使用Linux,那么也许您应该使用cron 每隔几分钟启动一次。
  • 我不确定,但这会在短时间内启动许多蜘蛛,并且会产生问题。
  • 您可以使用print() 查看哪些值有问题。您应该检查第一次运行或第二次运行是否有问题 - 当process.start() 在之前的process.stop() 之后再次执行时。也许所有问题都导致process.stop() 可能会杀死所有进程并且无法再次启动。
  • 我遇到了同样的问题,我通过以下问题解决了它Crochet

标签: python scrapy


【解决方案1】:

AFAIK 没有简单的方法可以重新启动蜘蛛,但有另一种方法 - 永远不会关闭的蜘蛛。为此,您可以使用spider_idle signal.

根据文档:

Sent when a spider has gone idle, which means the spider has no further:  
* requests waiting to be downloaded
* requests scheduled
* items being processed in the item pipeline

您也可以在官方documentation中找到使用Signals的示例。

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2021-12-19
    相关资源
    最近更新 更多