Scrapy:如何从其他python脚本运行两次或更多的蜘蛛?

Scrapy:如何从其他python脚本运行两次或更多的蜘蛛?,第1张

概述Scrapy版本:1.0.5 我已经搜索了很长时间,但大多数解决方法在当前的Scrapy版本中都不起作用. 我的蜘蛛是在jingdong_spider.py中定义的,界面(通过Scrapy Documentation学习它)来运行蜘蛛如下: # interfacedef search(keyword): configure_logging({'LOG_FORMAT': '%(leveln Scrapy版本:1.0.5

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

我的蜘蛛是在jingdong_spIDer.py中定义的,界面(通过Scrapy Documentation学习它)来运行蜘蛛如下:

# interfacedef 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中我将调用上面的搜索(关键字)来运行spIDer.

现在的问题是:我曾经调用过一次搜索(关键字),而且效果很好.但是我把它叫了两次,例如,

在temp.py

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

它报告说:

Traceback (most recent call last): file
“C:/Users/jiahao/Desktop/code/bbt_climb_plus/temp.py”,line 7,in

search(‘ipad2’) file “C:\Users\jiahao\Desktop\code\bbt_climb_plus\bbt_climb_plus\spIDers\jingdong_spIDer.py”,
line 194,in search
reactor.run() # the script will block here until the crawling is finished file
“C:\Python27\lib\site-packages\twisted\internet\base.py”,line 1193,
in run
self.startRunning(installSignalHandlers=installSignalHandlers) file “C:\Python27\lib\site-packages\twisted\internet\base.py”,line
1173,in startRunning
ReactorBase.startRunning(self) file “C:\Python27\lib\site-packages\twisted\internet\base.py”,line 684,in
startRunning
raise error.ReactorNotRestartable() twisted.internet.error.ReactorNotRestartable

第一次搜索(关键字)成功,但后者出错了.

你能帮忙吗?

解决方法 在您的代码示例中,您正在调用twisted.reactor,在每次函数调用时启动它.这不起作用,因为每个过程只有一个反应堆而你不能 start it twice.

有两种方法可以解决你的问题,这两种方法都在documentation here中描述.要么坚持使用CrawlerRunner,要么将reactor.run()移到search()函数之外,以确保它只被调用一次.或者使用CrawlerProcess并简单地调用crawler_process.start().第二种方法更容易,您的代码看起来像这样:

from scrapy.crawler import CrawlerProcessfrom dirbot.spIDers.dmoz import DmozSpIDerdef search(runner,keyword):    return runner.crawl(DmozSpIDer,keyword)runner = CrawlerProcess()search(runner,"alfa")search(runner,"beta")runner.start()
总结

以上是内存溢出为你收集整理的Scrapy:如何从其他python脚本运行两次或更多的蜘蛛?全部内容,希望文章能够帮你解决Scrapy:如何从其他python脚本运行两次或更多的蜘蛛?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1193621.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存