In Scrapy, you can pass parameters to Spiders by using the custom settings of Scrapy Spider. You can use the __init__
method of the Spider class to accept parameters.
Here is how you can do it:
import scrapy
class MySpider(scrapy.Spider):
name = 'myspider'
def __init__(self, category=None, *args, **kwargs):
super(MySpider, self).__init__(*args, **kwargs)
self.start_urls = ['http://www.example.com/categories/%s' % category]
In the above code, the __init__
method is used to accept the parameters. The category
parameter is passed to the Scrapy spider and used to construct the start_urls
.
Now, when you run the Scrapy spider, you can pass the parameter from the command line:
scrapy crawl myspider -a category=electronics
In the above command, myspider
is the name of the Spider and category
is the parameter being passed to the Spider. The -a
option is used to pass parameters to the Spider.