我想爲自定義基本蜘蛛類中的蜘蛛提供一些通用功能。定製BaseSpider Scrapy
通常scrapy蜘蛛繼承scrapy.Spider
類。
我試圖創造scrapy的蜘蛛文件夾BaseSpider類,沒有工作
import scrapy
class BaseSpider(scrapy.Spider):
def __init__(self):
super(scrapy.Spider).__init__()
def parse(self, response):
pass
這裏是我的實際蜘蛛
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
給出了這樣的錯誤
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)
然後,我試圖使用多繼承,並讓我的易趣蜘蛛看起來像
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
這給
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
我在Python以及scrapy新的,我想實現我在這其中不工作我想編碼的PHP風格。
我正在尋找適當的方法。
感謝
更新
改變了初始化簽名按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)
仍然得到
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)
你有__init __()嗎? – omdv
init在哪個類中? –
您在EbaySpider上的第一個錯誤表明__init__存在問題。你是如何定義它的? – omdv