我正在尝试将用户定义的参数传递给scrapy的Spider。谁能建议该怎么做?我在-a某处读到一个参数,但不知道如何使用它。
3 回答

慕姐8265434
TA贡献1813条经验 获得超2个赞
先前的答案是正确的,但是您不必__init__每次想要编写scrapy的Spider时都声明构造函数(),只需像以前一样指定参数即可:
scrapy crawl myspider -a parameter1=value1 -a parameter2=value2
在您的蜘蛛代码中,您可以将它们用作蜘蛛参数:
class MySpider(Spider):
name = 'myspider'
...
def parse(self, response):
...
if self.parameter1 == value1:
# this is True
# or also
if getattr(self, parameter2) == value2:
# this is also True
而且就可以了。
添加回答
举报
0/150
提交
取消