I am trying to pass a user defined argument to a scrapy's spider. Can anyone suggest on how to do that?
I read about a parameter -a
somewhere but have no idea how to use it.
I am trying to pass a user defined argument to a scrapy's spider. Can anyone suggest on how to do that?
I read about a parameter -a
somewhere but have no idea how to use it.
Spider arguments are passed in the
crawl
command using the-a
option. For example:Spiders can access arguments as attributes:
Taken from the Scrapy doc: http://doc.scrapy.org/en/latest/topics/spiders.html#spider-arguments
Update 2013: Add second argument
Update 2015: Adjust wording
Update 2016: Use newer base class and add super, thanks @Birla
Update 2017: Use Python3 super
Update 2018: As @eLRuLL points out, spiders can access arguments as attributes
Spider arguments are passed while running the crawl command using the -a option. For example if i want to pass a domain name as argument to my spider then i will do this-
And receive arguments in spider's constructors:
...
it will work :)
Previous answers were correct, but you don't have to declare the constructor (
__init__
) every time you want to code a scrapy's spider, you could just specify the parameters as before:and in your spider code you can just use them as spider arguments:
And it just works.
To pass arguments with crawl command
To pass arguments to run on scrapyd replace -a with -d
The spider will receive arguments in its constructor.
Scrapy puts all the arguments as spider attributes and you can skip the init method completely. Beware use getattr method for getting those attributes so your code does not break.