Using arguments in scrapy pipeline on __init__(在 __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 管道中使用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 __init__ 上的 scrapy 管道中使用参数


基础教程推荐
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01