自定义 BaseSpider Scrapy
Custom BaseSpider Scrapy
我想在自定义基础蜘蛛中为蜘蛛提供一些通用功能 class。
通常 scrapy spider 继承自 scrapy.Spider
class.
我尝试在 scrapy 的 spiders 文件夹中创建一个 BaseSpider class 但没有成功
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)
然后我尝试使用多重继承并使我的 ebay 蜘蛛看起来像
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
更改了 init 签名
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)
看看scrapy.Spider.__init__
signature:
def __init__(self, name=None, **kwargs):
# ...
Subclasses 应该定义具有相同签名的 __init__
方法。如果您不关心 name 和 kwargs,只需将它们传递给 base class:
class BaseSpider(scrapy.Spider):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def parse(self, response):
pass
如果 EbaySpider 已经继承自 BaseSpider,则它不必继承自 scrapy.Spider。它也应该具有相同的 __init__
签名,并且还需要调用 super()
:
class EbaySpider(BaseSpider):
name = "ebay"
allowed_domains = ["ebay.com"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.redis = Redis(host='redis', port=6379)
(我对 super()
使用 Python 3 语法)
编辑
还有一个问题:您正在像这样导入 BaseSpider:
import BaseSpider
可能您有一个名为 BaseSpider 的模块(BaseSpider.py 文件),并且此模块中有一个名为 BaseSpider 的 class。 import BaseSpider
给你 module 对象,而不是蜘蛛 class。尝试使用 from BaseSpider import BaseSpider
,最好重命名模块以避免混淆并遵循 pep-8.
我想在自定义基础蜘蛛中为蜘蛛提供一些通用功能 class。
通常 scrapy spider 继承自 scrapy.Spider
class.
我尝试在 scrapy 的 spiders 文件夹中创建一个 BaseSpider class 但没有成功
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)
然后我尝试使用多重继承并使我的 ebay 蜘蛛看起来像
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
更改了 init 签名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)
看看scrapy.Spider.__init__
signature:
def __init__(self, name=None, **kwargs):
# ...
Subclasses 应该定义具有相同签名的 __init__
方法。如果您不关心 name 和 kwargs,只需将它们传递给 base class:
class BaseSpider(scrapy.Spider):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def parse(self, response):
pass
如果 EbaySpider 已经继承自 BaseSpider,则它不必继承自 scrapy.Spider。它也应该具有相同的 __init__
签名,并且还需要调用 super()
:
class EbaySpider(BaseSpider):
name = "ebay"
allowed_domains = ["ebay.com"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.redis = Redis(host='redis', port=6379)
(我对 super()
使用 Python 3 语法)
编辑
还有一个问题:您正在像这样导入 BaseSpider:
import BaseSpider
可能您有一个名为 BaseSpider 的模块(BaseSpider.py 文件),并且此模块中有一个名为 BaseSpider 的 class。 import BaseSpider
给你 module 对象,而不是蜘蛛 class。尝试使用 from BaseSpider import BaseSpider
,最好重命名模块以避免混淆并遵循 pep-8.