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

查看:356
本文介绍了在__init__上的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:是的,我尝试在管道类中使用诸如spyer.py之类的super()函数,但它不起作用.

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() 方法中阅读它们:

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天全站免登陆