在\uyu init上使用scrapy pipeline中的参数__

2024-06-26 01:54:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有点痒管道.py我想得到给定的论点。在我的蜘蛛网.py它工作完美:

class MySpider( CrawlSpider ):
    def __init__(self, host='', domain_id='', *args, **kwargs):

        super(MySpider, self).__init__(*args, **kwargs)
        print user_id
        ...

现在,我需要在我的管道.py创建类似“domain-123.db”的sqlite数据库。我在网上搜索我的问题,但找不到任何解决办法。在

有人能帮我吗?在

PS:是的,我在pipelines类中尝试了super()函数,比如间谍.py,它不起作用。在


Tags: pyselfidhost管道initdomaindef
2条回答

我可能已经太晚了,无法为op提供一个有用的答案,但是对于将来遇到这个问题的任何人(就像我一样),您应该检查类方法from_crawler和/或{}。在

这样你就可以按照你想要的方式传递你的论点。在

检查: https://doc.scrapy.org/en/latest/topics/item-pipeline.html#from_crawler

from_crawler(cls, crawler)

If present, this classmethod is called to create a pipeline instance from a Crawler. It must return a new instance of the pipeline. Crawler object provides access to all Scrapy core components like settings and signals; it is a way for pipeline to access them and hook its functionality into Scrapy.

Parameters: crawler (Crawler` object) – crawler that uses this pipeline

{cd1>参数集中的参数:

class MySpider(CrawlSpider):
    def __init__(self, user_id='', *args, **kwargs):
        self.user_id = user_id

        super(MySpider, self).__init__(*args, **kwargs) 

并在管道的^{}方法中读取它们:

^{2}$

相关问题 更多 >