我如何在 Scrapy 中将我的蜘蛛组织到嵌套目录中?

How can I organize my spiders into nested directories in Scrapy?

我有以下目录结构:

my_project/
  __init__.py
  spiders/
    __init__.py
    my_spider.py
    other_spider.py
  pipeines.py
  # other files

现在我可以在 my_project 目录中并使用 scrapy crawl my_spider 开始我的抓取。

我想要实现的是能够 运行 scrapy crawl my_spider 使用这个更新的结构:

my_project/
  __init__.py
  spiders/
    __init__.py
    subtopic1/
      __init__.py # <-- I get the same error whether this is present or not
      my_spider.py
    subtopicx/
      other_spider.py
  pipeines.py
  # other files

但是现在我收到这个错误:

KeyError: 'Spider not found: my_spider'

将Scrapy蜘蛛组织到目录中的合适方式是什么?

您必须从包含 scrapy.cfg 的目录中 运行 这个 scrapy crawl my_spider。您不会收到任何错误。

my_project/
  __init__.py
  spiders/
    __init__.py
     my_spider.py
     sub_directory
       __init__.py  
       other_spider.py
  pipeines.py
scrapy.cfg

我知道这早就应该了,但这是在嵌套目录中组织蜘蛛的正确方法。您在定义的设置中设置模块位置 here.

示例:

SPIDER_MODULES = ['my_project.spiders', 'my_project.spiders.subtopic1', 'my_project.spiders.subtopicx']