scrapy 之自定义命令运行所有爬虫文件
1、在spider文件夹同级目录创建commands python包
2、在包下创建command.py文件
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。3、从scrapy.commands包下引入ScrapyCommand
4、创建一个类,继承ScrapyCommand
5、重新定义类变量 requires_project = True
6、重写syntax short_desc方法,syntax返回空字符串 short_desc返回描述字符串
7、重写run方法。
8、在settings.py 中添加配置 COMMANDS_MODULE = '项目名称.目录名称'
def run(self, args, opts): spider_list = self.crawler_process.spiders.list() #通过self.crawler_process.spider.list()获得所有爬虫 for name in spider_list: #遍历所有爬虫 self.crawler_process.crawl(name, **opts.__dict__) #运行爬虫 self.crawler_process.start() #启动进程
crawler_process 来自父类
更多精彩