Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to scrapy crawler from scrapyd?

I can run a spider in scrapy with a simple command

scrapy crawl custom_spider -a input_val=5 -a input_val2=6

where input_val and input_val2 are the values i'm passing to the spider

and the above method works fine..

However while scheduling a spider with scrapyd

running

curl http://localhost:6800/schedule.json -d project=crawler -d input_val=5 -d input_val2=6 -d spider=custom_spider

Throws an error

spider = cls(*args, **kwargs)
    exceptions.TypeError: __init__() got an unexpected keyword argument '_job'

How do i get this to work?

Edit This: is inside my initializer:

def __init__(self,input_val=None, input_val2=None, *args, **kwargs):
        self.input_val = input_val
        self.input_val2 = input_val2
        super(CustomSpider, self).__init__(*args, **kwargs)
like image 359
wolfgang Avatar asked Aug 26 '15 10:08

wolfgang


1 Answers

Be sure to support arbitrary keyword arguments in your spider and call __init__ with super() like shown in the docs for spider arguments:

class MySpider(scrapy.Spider):
    name = 'myspider'

    def __init__(self, category=None, *args, **kwargs):
        super(MySpider, self).__init__(*args, **kwargs) # <- important
        self.category = category

Scrapyd supplies the job ID as a _job argument passed to the spider (see code here).

like image 128
Elias Dorneles Avatar answered Oct 06 '22 11:10

Elias Dorneles