Scrapy: 在管道之间传递实例变量

huangapple go评论61阅读模式
英文:

Scrapy: passing instance variables between pipelines

问题

在Pipeline_1中,我在init方法中使用spider.variable=[]定义了一个爬虫实例变量,并在同一管道的close_spider方法中填充它。

在Pipeline_2中,我在spider_closed方法中(也尝试过close_spider)尝试访问它,但它是空的。

在pipeline_1中,我正在处理一些项目,我需要一次性将它们全部传递给pipeline_2,这是我一直在考虑的唯一解决方案。

英文:

Does passing spider instance variables between pipelines work?
Unfortunately I do not have the code but I'll try to explain as short and clear as possible.

Order is the following:
Pipeline_1: high priority (@700)
Pipeline_2: low priority (@900)

In Pipeline_1 I'm defining a spider instance variable with spider.variable=[] in init method and filling it in close_spider method of the same pipeline.

In Pipeline_2 I'm accessing it in spider_closed method (tried close_spider too) but it is empty.

In pipeline_1 I'm processing some items and I need to pass them all at once in pipeline_2 and this is the only solution I've been thinking about.

答案1

得分: 0

所以在更多阅读后,我发现在管道中定义实例变量并不是一个很好的主意(亚历山大也提到了这一点) - 我通过将pipeline_2的方法实现到pipeline_1中,并在close_spider方法中完成所有操作来解决了这个问题。

英文:

So after reading a bit more I found that defining an instance variable in the pipelines is not that great of an idea (also what Alexander mentioned) - I've solved the issue by implementing the pipeline_2 methods into pipeline_1 and do everything in close_spider method.

huangapple
  • 本文由 发表于 2023年2月16日 05:14:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定