【问题标题】:Scrapy: How to run spider from other python script twice or more?Scrapy:如何从其他python脚本运行蜘蛛两次或更多次?
【发布时间】:2016-07-24 22:02:23
【问题描述】:

Scrapy 版本:1.0.5

我已经搜索了很长时间,但是大多数解决方法在当前的 Scrapy 版本中都不起作用。

我的spider在jingdong_spider.py中定义,运行spider的接口(通过Scrapy Documentation学习)如下:

# interface
def search(keyword):
    configure_logging({'LOG_FORMAT': '%(levelname)s: %(message)s'})
    runner = CrawlerRunner()
    d = runner.crawl(JingdongSpider,keyword)
    d.addBoth(lambda _: reactor.stop())
    reactor.run() # the script will block here until the crawling is finished

然后在temp.py中调用上面的search(keyword)来运行spider。

现在的问题是:我调用了一次 search(keyword),它运行良好。但我调用了两次,例如,

在 temp.py 中

search('iphone')
search('ipad2')

它报告了:

Traceback(最近一次调用最后一次):文件 “C:/Users/jiahao/Desktop/code/bbt_climb_plus/temp.py”,第 7 行,在 search('ipad2') 文件 "C:\Users\jiahao\Desktop\code\bbt_climb_plus\bbt_climb_plus\spiders\jingdong_spider.py", 第 194 行,搜索中 reactor.run() # 脚本会在这里阻塞,直到爬取完成 File “C:\Python27\lib\site-packages\twisted\internet\base.py”,第 1193 行, 运行中 self.startRunning(installSignalHandlers=installSignalHandlers) 文件“C:\Python27\lib\site-packages\twisted\internet\base.py”,行 1173,在开始运行 ReactorBase.startRunning(self) 文件“C:\Python27\lib\site-packages\twisted\internet\base.py”,第 684 行,在 开始跑步 raise error.ReactorNotRestartable() twisted.internet.error.ReactorNotRestartable

第一次搜索(keyword)成功,后面找错了。

你能帮忙吗?

【问题讨论】:

    标签: python python-2.7 scrapy twisted


    【解决方案1】:

    在您的代码示例中,您正在调用 twisted.reactor 在每个函数调用时启动它。这不起作用,因为每个进程只有一个反应器,您不能start it twice

    有两种方法可以解决您的问题,都在documentation here 中描述。要么坚持使用CrawlerRunner,但将reactor.run() 移到search() 函数之外,以确保它只被调用一次。或者使用CrawlerProcess 并直接调用crawler_process.start()。第二种方法更简单,您的代码如下所示:

    from scrapy.crawler import CrawlerProcess
    from dirbot.spiders.dmoz import DmozSpider
    
    def search(runner, keyword):
        return runner.crawl(DmozSpider, keyword)
    
    runner = CrawlerProcess()
    search(runner, "alfa")
    search(runner, "beta")
    runner.start()
    

    【讨论】:

    • 谢谢。您的回答很棒,但我在输入内容时需要使用search(keyword)。即search(keyword)需要在用户做某事时被调用(比如点击按钮等)。这样search(keyword)不能硬编码。最后,我用多处理解决了我的问题。
    • 恐怕我没有解释清楚。 search(keyword) 可能会被调用两次、三次、四次......这取决于。
    • 在某些函数中出现错误.. processCrawl = CrawlerProcess(get_project_settings()),当我再次调用时,它说twisted.internet.error.ReactorNotRestartable
    【解决方案2】:

    正如 Pawel Miech 所说的

    在您的代码示例中,您正在调用twisted.reactor 它在每个函数调用上。这不起作用,因为只有 每个进程一个反应器,你不能启动它两次。

    我找到了解决问题的方法。只是使用多处理。

    会是这样的:

    from multiprocessing import Process
    def run_spider(keyword):
        if __name__ == '__main__':
            p = Process(target=jingdong_spider.search, args=(keyword.encode('utf-8'),))
            p.start()
            p.join()
    

    如果每个人在使用 python-multiprocessing 时都有问题。最好看一下 python 文档。

    【讨论】:

    • 虽然这是一个悬而未决的解决方法,但效果很好。你拯救了我的一天!我有 50 分钟的时间来完成我的项目。
    • 我出错了.. p.start() File "/usr/local/lib/python3.6/multiprocessing/process.py", line 103, in start 'daemonic processes are not allowed to have children'
    猜你喜欢
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2020-08-12
    相关资源
    最近更新 更多