程序地址:https://github.com/werbenhu/WechatSpider
最近在写爬取新闻的爬虫,但是发现scrapy不支持一次同时启动多个spider,到网上查找了各种资料,可能是版本的问题均不得法。
有说用scrapyd来调度scrapy的,也搭建试用了一下scrapyd,感觉还是有点麻烦,用法有点别扭。
还是自己从源码下手,既然能调用指定的spider,为什么不能同时执行多个spider呢?
在spider的parse(self, response)处设置一个断点,查看调用堆栈,
看上图中的调用堆栈,从顶至下一级一级的看代码,在crawl.py line55行处看到如下代码
def run(self, args, opts):
if len(args) < 1:
raise UsageError()
elif len(args) > 1:
raise UsageError("running 'scrapy crawl' with more than one spider is no longer supported")
spname = args[0]
“running ‘scrapy crawl’ with more than one spider is no longer supported”看到这里没有,这里限制了spider的数量,将其修改成如下
def run(self, args, opts):
if len(args) < 1:
raise UsageError()
# elif len(args) > 1:
# raise UsageError("running 'scrapy crawl' with more than one spider is no longer supported")
# spname = args[0]
for spname in args:
self.crawler_process.crawl(spname, **opts.spargs)
self.crawler_process.start()
然后crawl ThirtySix One Two就可以同时执行三个ThirtySix 、One 、Two三个spider了,
但是三个spider公用的是一个setting.py文件,这个还需要再研究,看能不能每个spider都用自己的setting文件。