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 管道中使用参数
基础教程推荐
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
