在 for 循环中运行多个蜘蛛 [英] Running more than one spider in a for loop

查看:50
本文介绍了在 for 循环中运行多个蜘蛛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实例化多个蜘蛛.第一个工作正常,但第二个给了我一个错误:ReactorNotRestartable.

I try to instantiate multiple spiders. The first one works fine, but the second one gives me an error: ReactorNotRestartable.

feeds = {
    'nasa': {
        'name': 'nasa',
        'url': 'https://www.nasa.gov/rss/dyn/breaking_news.rss',
        'start_urls': ['https://www.nasa.gov/rss/dyn/breaking_news.rss']
    },
    'xkcd': {
        'name': 'xkcd',
        'url': 'http://xkcd.com/rss.xml',
        'start_urls': ['http://xkcd.com/rss.xml']
    }    
}

对于上述项目,我尝试循环运行两个蜘蛛,如下所示:

With the items above, I try to run two spiders in a loop, like this:

from scrapy.crawler import CrawlerProcess
from scrapy.spiders import XMLFeedSpider

class MySpider(XMLFeedSpider):

    name = None

    def __init__(self, **kwargs):

        this_feed = feeds[self.name]
        self.start_urls = this_feed.get('start_urls')
        self.iterator = 'iternodes'
        self.itertag = 'items'
        super(MySpider, self).__init__(**kwargs)

def parse_node(self, response, node):
    pass


def start_crawler():
    process = CrawlerProcess({
        'USER_AGENT': CONFIG['USER_AGENT'],
        'DOWNLOAD_HANDLERS': {'s3': None} # boto issues
    })

    for feed_name in feeds.keys():
        MySpider.name = feed_name
        process.crawl(MySpider)
        process.start() 

第二个循环的异常看起来像这样,蜘蛛打开了,但是:

The exception for the second loop looks like this, spider opened, but then:

...
2015-11-22 00:00:00 [scrapy] INFO: Spider opened
2015-11-22 00:00:00 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-11-22 00:00:00 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-11-21 23:54:05 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
Traceback (most recent call last):
  File "env/bin/start_crawler", line 9, in <module>
    load_entry_point('feed-crawler==0.0.1', 'console_scripts', 'start_crawler')()
  File "/Users/bling/py-feeds-crawler/feed_crawler/crawl.py", line 51, in start_crawler
    process.start() # the script will block here until the crawling is finished
  File "/Users/bling/py-feeds-crawler/env/lib/python2.7/site-packages/scrapy/crawler.py", line 251, in start
    reactor.run(installSignalHandlers=False)  # blocking call
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1193, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1173, in startRunning
    ReactorBase.startRunning(self)
  File "/usr/local/lib/python2.7/site-packages/twisted/internet/base.py", line 684, in startRunning
    raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable

我是否必须使第一个 MySpider 无效,或者我做错了什么,需要改变它的工作原理.提前致谢.

Do I have to invalidate the first MySpider or what am I doing wrong and need to change that it works. Thanks in advance.

推荐答案

解决方案是收集循环中的蜘蛛并在结束时只启动一次进程.我的猜测,这与 Reactor 分配/解除分配有关.

Solution is to collect the spiders in the loop and start process just once at the end. My guess, it has something to do with the Reactor allocation / deallocation.

def start_crawler():

    process = CrawlerProcess({
        'USER_AGENT': CONFIG['USER_AGENT'],
        'DOWNLOAD_HANDLERS': {'s3': None} # disable for issues with boto
    })

    for feed_name in CONFIG['Feeds'].keys():
        MySpider.name = feed_name
        process.crawl(MySpider)

    process.start()

感谢@eLRuLL 的回答,它激发了我找到此解决方案的灵感.

Thanks @eLRuLL for your answer it inspired me to find this solution.

这篇关于在 for 循环中运行多个蜘蛛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆