Scrapy:无法覆盖__init__function
问题描述:
我创建了一个从CrawlSpider继承的蜘蛛。Scrapy:无法覆盖__init__function
我需要使用__init__
功能,但总是收到此错误:
代码:
class mySpider(CrawlSpider):
def __init__(self):
super(mySpider, self).__init__()
.....
这是我得到的错误: KeyError异常蜘蛛没有发现:mySpider。
没有__init__
功能一切正常
答
你需要把它像这样:
def __init__(self, *a, **kw):
super(MySpider, self).__init__(*a, **kw)
# your code here
工作例如:
class MySpider(CrawlSpider):
name = "company"
allowed_domains = ["site.com"]
start_urls = ["http://www.site.com"]
def __init__(self, *a, **kw):
super(MySpider, self).__init__(*a, **kw)
dispatcher.connect(self.spider_closed, signals.spider_closed)
这里的init,使用了蜘蛛注册scrapy信号,我在这个例子中需要蜘蛛而不是通常在管道中
'super'的语义有时可能会让人困惑。试试'CrawlSpider .__ init __(self)'。 – 2012-07-21 17:53:05