python - 从循环内部运行scrapy spider
问题描述
我有一个程序从RabbitMQ 的队列中获取输入,然后运行一个通过这个输入的爬虫。它工作正常,但只是第一次,在第一次输入之后它会抛出这个错误:
twisted.internet.error.ReactorNotRestartable
这是我的代码:
def start_flow(crawler_class):
# Create the queue and all the connections
queue = Queue(connection_uri='amqp://guest:guest@localhost:5672/')
queue.create_connection()
queue.create_channel()
queue_name = _get_queue_name(crawler_class)
queue.define_queue(queue_name)
# ---------------------------------------
while True:
print("Waiting for the next input")
# Get an input
queue.consume(queue_name)
queue.channel.start_consuming()
input_ = queue.get_next_input()
# ------------
# Runs the crawler passing the input
runner = CrawlerRunner()
d = runner.crawl(crawler_class, input=input_)
d.addCallback(lambda _: reactor.stop())
reactor.run()
# ----------------------------
我已经尝试过 CrawlerProcess,但它引发了同样的问题
解决方案
推荐阅读
- python - 了解 Pandas Pivot 函数
- node.js - 如何使用两个令牌(访问/刷新)进行授权(nodejs,express)
- html - Excel VBA 填写网络表单并检索结果
- python - PyQt 通过点击 QPushButton 更改背景窗口
- reactjs - 在布局中设置 Material-UI 垂直和居中对齐的 Grid 项目
- arrays - C结构类型变量声明
- r - 如何将函数的输出存储在列表中
- php - 数组值总是被 foreach 中的最后一个键覆盖
- mule - Mule 与 JIRA 的集成
- c# - 如何使用 .Net 5 Core 在其他类中全局访问 appsettings.json