Scrapy:一次性运行多个Spiders

http://blog.csdn.net/iefreer/article/details/20677943

如果创建了多个Spider,希望通过cronjob一次性运行所有的Spiders,可以通过自定义Scrapy命令来实现。

1. 在你的Scrapy工程下面新建一个目录:

cd path/to/your_project

mkdir commands

注意这个commands和spiders目录是同级的


2. 在commands下面添加一个文件crawlall.py,代码如下:

[python]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. from scrapy.command import ScrapyCommand  
  2. from scrapy.utils.project import get_project_settings  
  3. from scrapy.crawler import Crawler  
  4.   
  5. class Command(ScrapyCommand):  
  6.   
  7.     requires_project = True  
  8.   
  9.     def syntax(self):  
  10.         return '[options]'  
  11.   
  12.     def short_desc(self):  
  13.         return 'Runs all of the spiders'  
  14.   
  15.     def run(self, args, opts):  
  16.         settings = get_project_settings()  
  17.   
  18.         for spider_name in self.crawler.spiders.list():  
  19.             crawler = Crawler(settings)  
  20.             crawler.configure()  
  21.             spider = crawler.spiders.create(spider_name)  
  22.             crawler.crawl(spider)  
  23.             crawler.start()  
  24.   
  25.         self.crawler.start()  

3. 在settings.py中添加配置:

COMMANDS_MODULE = 'yourprojectname.commands'


4. 在cronjob中添加:scrapy crawlall命令即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值