scrapy框架运行多个爬虫

别说谁变了你拦得住时间么 提交于 2021-02-04 19:29:42

run.py配置以下内容,仅仅可以运行单一爬虫,如果想要一次运行多个爬虫 ,就需要换一种方式。

from scrapy import cmdline

"""
单一爬虫运行
"""
cmdline.execute('scrapy crawl runoob'.split())
  • 第一步:
    from scrapy.commands import ScrapyCommand
    from scrapy.utils.project import get_project_settings
    
    """
    运行多个爬虫
    """
    
    class Command(ScrapyCommand):
        requires_project = True
    
        def syntax(self):
            return '[options]'
    
        def short_desc(self):
            return 'Runs all of the spiders'
    
        def run(self, args, opts):
            spider_list = self.crawler_process.spiders.list()
            for name in spider_list:
                self.crawler_process.crawl(name, **opts.__dict__)
            self.crawler_process.start()

     

  • 第二步setting添加如下代码:
    # 运行多个爬虫设置
    COMMANDS_MODULE = 'Runoob_Redis.commands'

     

  • 执行----->

    scrapy crawlall

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!