在__init__上使用scrapy管道中的参数

use*_*915 7 python arguments scrapy web-scraping scrapy-spider

我有一个scrapy pipelines.py,我想得到给定的参数.在我的spider.py中,它完美无缺:

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

        super(MySpider, self).__init__(*args, **kwargs)
        print user_id
        ...
Run Code Online (Sandbox Code Playgroud)

现在,我需要在pipelines.py中使用"user_id"来创建像"domain-123.db"这样的sqlite数据库.我在整个网络上搜索我的问题,但我找不到任何解决方案.

有人能帮我吗?

PS:是的,我尝试使用我的管道类中的super()函数,如spyer.py,它不起作用.

ale*_*cxe 12

spider构造函数中设置参数:

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

        super(MySpider, self).__init__(*args, **kwargs) 
Run Code Online (Sandbox Code Playgroud)

并在open_spider()您的管道方法中读取它们:

def open_spider(self, spider):
    print spider.user_id
Run Code Online (Sandbox Code Playgroud)