I want to have some generic functionalities for spiders in a custom base spider class.
Usually scrapy spiders inherit from scrapy.Spider class.
I tried creating a BaseSpider class in the spiders folder of scrapy which didn't work
import scrapy
class BaseSpider(scrapy.Spider):
def __init__(self):
super(scrapy.Spider).__init__()
def parse(self, response):
pass
And here is my actual spider
import scrapy
import BaseSpider
class EbaySpider(BaseSpider):
name = "ebay"
allowed_domains = ["ebay.com"]
def __init__(self):
self.redis = Redis(host='redis', port=6379)
# rest of the spider code
Gives this error
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
Then i tried to use multi inheritance and make my ebay spider looks like
class EbaySpider(scrapy.Spider, BaseSpider):
name = "ebay"
allowed_domains = ["ebay.com"]
def __init__(self):
self.redis = Redis(host='redis', port=6379)
# rest of the spider code
which gives
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
I am new in python as well as scrapy and i am trying to implement my PHP style of coding in it which isn't working i guess.
I am looking for a proper approach.
Thanks
Updated
Changed the init signature as per scrapy.Spider
BaseSpider
def __init__(self, *args, **kwargs):
super(scrapy.Spider, self).__init__(*args, **kwargs)
EbaySpider
class EbaySpider(BaseSpider):
def __init__(self, *args, **kwargs):
super(BaseSpider,self).__init__(*args, **kwargs)
self.redis = Redis(host='redis', port=6379)
Still getting
File "/scrapper/scrapper/spiders/ebay.py", line 11, in <module>
class EbaySpider(BaseSpider):
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)