我正在按照本指南http://doc.scrapy.org/en/0.16/topics/practices.html#run-scrapy-from-a-script运行脚本中的scrapy。 以下是我剧本的一部分:
crawler = Crawler(Settings(settings))
crawler.configure()
spider = crawler.spiders.create(spider_name)
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run()
print "It can't be printed out!"
它的工作原理应该是:访问页面、收集所需信息并将输出json存储在我告诉它的地方(通过FEED-URI)。但当spider完成工作(我可以在输出json中看到数字)时,我的脚本将不会继续执行。 也许这不是小问题。答案应该在twisted的反应堆里。 如何释放线程执行?
在scrapy 0.19.x中,您应该执行以下操作:
注意这些行
没有它,你的蜘蛛将不会使用你的设置,也不会保存项目。 我花了一段时间才弄明白为什么文档中的示例没有保存我的项目。我发送了一个请求来修复doc示例。
另一种方法是直接从脚本调用命令
蜘蛛完成后你需要停止反应器。您可以通过监听
spider_closed
信号来实现这一点:命令行日志输出可能类似于:
相关问题 更多 >
编程相关推荐