Skip to main content
 首页 » 编程设计

python之在 Scrapy 中获取所有蜘蛛类名称

2025年05月04日13lori

在旧版本中,我们可以使用以下代码获取蜘蛛列表(蜘蛛名称),但在当前版本(1.4)中,我遇到了

[py.warnings] WARNING: run-all-spiders.py:17: ScrapyDeprecationWarning: CrawlerRunner.spiders attribute is renamed to CrawlerRunner.spider_loader. 
for spider_name in process.spiders.list(): 
    # list all the available spiders in my project 

使用crawler.spiders.list():

>>> for spider_name in crawler.spiders.list(): 
...     print(spider_name) 

如何在 Scrapy 中获取蜘蛛列表(和等效的类名)?

请您参考如下方法:

我在我的实用程序脚本中使用它来运行蜘蛛:

from scrapy import spiderloader 
from scrapy.utils import project 
 
settings = project.get_project_settings() 
spider_loader = spiderloader.SpiderLoader.from_settings(settings) 
spiders = spider_loader.list() 
classes = [spider_loader.load(name) for name in spiders] 

在您的情况下,按照警告消息的建议将 spiders 重命名为 spider_loader 就足够了。