scrapy 之自定義命令運行所有爬蟲文件
阿新 • • 發佈:2019-04-15
lis roc syn span 變量 def .com 一個 req
1、在spider文件夾同級目錄創建commands python包
2、在包下創建command.py文件
3、從scrapy.commands包下引入ScrapyCommand
4、創建一個類,繼承ScrapyCommand
5、重新定義類變量 requires_project = True
6、重寫syntax short_desc方法,syntax返回空字符串 short_desc返回描述字符串
7、重寫run方法。
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 來自父類
scrapy 之自定義命令運行所有爬蟲文件