一開始我們默認都是只有一個爬蟲的,所以執行的代碼都是在項目下創建一個py文件
from scrapy import cmdline
cmdline.execute('scrapy crawl 爬蟲名'.split( ))
但是要執行多個爬蟲就犯難了,在這里我只是做個筆記加強記憶
原博客 https://www.cnblogs.com/lei0213/p/7900340.html
其中執行以下:
1、在spiders同級創建任意目錄,如:commands
2、在其中創建 crawlall.py 文件 (此處文件名就是自定義的命令)
crawlall.py
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()
到這里還沒完,settings.py配置文件還需要加一條。
COMMANDS_MODULE = ‘項目名稱.目錄名稱’
項目名 目錄名稱 COMMANDS_MODULE = 'zhihuuser.commands'
這就是幾乎完成了,如果需要執行,那么只要在cmd中cd進項目中scrapy crawlall,或者項目下新建一個py文件使用scrapy.cmdline運行, 或者 os.system('scrapy crawlall')