我正在通过继承RFPDupeFilter
来创建自定义过滤器。
这是我正在使用代码的链接:
https://github.com/j4s0nh4ck/wiki-spider/blob/master/wiki/wiki/SeenURLFilter.py
注意:我在settings.py所在的同一目录中的custom_filters.py自定义文件中有上面的代码,然后在settings.py中我有这段代码。
DUPEFILTER_CLASS = 'myspider.custom_filters.SeenURLFilter'
但是当我运行机器人时,我收到了这个错误:
exceptions.TypeError:
__init__()
只取1个参数(给定3个)
答案 0 :(得分:1)
正如您在调用过滤器的追溯from_settings()
方法中所看到的那样,它会创建自定义重叠过滤器的实例。但是,由于您没有指定自己的from_settings()
方法the one from built-in RFPDupeFilter
is used:
@classmethod
def from_settings(cls, settings):
debug = settings.getbool('DUPEFILTER_DEBUG')
return cls(job_dir(settings), debug)
尝试使用path
和debug
构造函数参数实例化自定义重复过滤器。并且您的SeenURLFilter
构造函数不接受debug
参数。
你需要让你的dupefilter接受debug
parameter:
from scrapy.dupefilter import RFPDupeFilter
class SeenURLFilter(RFPDupeFilter):
"""A dupe filter that considers the URL"""
def __init__(self, path=None, debug=False): # FIX WAS APPLIED HERE
self.urls_seen = set()
RFPDupeFilter.__init__(self, path, debug) # AND HERE
def request_seen(self, request):
if request.url in self.urls_seen:
return True
else:
self.urls_seen.add(request.url)