在 __init__ 上使用 scrapy 管道中的参数 [英] Using arguments in scrapy pipeline on __init__

查看:38
本文介绍了在 __init__ 上使用 scrapy 管道中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个scrapy pipelines.py,我想获取给定的参数.在我的 spider.py 中它完美运行:

i have a scrapy pipelines.py and i want to get the given arguments. In my spider.py it works perfect:

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

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

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

Now, i need the "user_id" in my pipelines.py to create the sqlite database like "domain-123.db". I search the whole web about my problem, but i cant find any solution.

有人可以帮我吗?

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

PS: Yes, i try'ed the super() function within my pipelines Class like the spyer.py, it dont work.

推荐答案

spider 的构造函数中设置参数:

Set the arguments inside the spider's constructor:

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

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

并在 open_spider()a> 管道的方法:

And read them in the open_spider() method of your pipeline:

def open_spider(self, spider):
    print spider.user_id

这篇关于在 __init__ 上使用 scrapy 管道中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆