【问题标题】:Scrapy error: TypeError: __init__() got an unexpected keyword argument 'callback'Scrapy 错误:TypeError:__init__() 得到了意外的关键字参数“回调”
【发布时间】:2016-07-12 17:29:39
【问题描述】:

我正在尝试通过提取所有带有“huis”(荷兰语中的“house”)的链接来抓取网站。关注http://doc.scrapy.org/en/latest/topics/spiders.html,我正在尝试

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

from Funda.items import FundaItem

class FundaSpider(scrapy.Spider):
    name = "Funda"
    allowed_domains = ["funda.nl"]
    start_urls = [
        "http://www.funda.nl/koop/amsterdam/"
    ]

    rules = (
    Rule(LinkExtractor(allow=r'.*huis.*', callback='parse_item'))
    )

    def parse_item(self, response):
        item = FundaItem()
        item['title'] = response.extract()
        return item

但是,我收到了错误消息

Rule(LinkExtractor(allow=r'.*huis.*', callback='parse_item'))
TypeError: __init__() got an unexpected keyword argument 'callback'

从之前的帖子 (Scrapy Error: TypeError: __init__() got an unexpected keyword argument 'deny') 看来,可能的原因是括号不匹配,因此关键字被传递给 Rule 而不是 LinkExtractor。但是,在我看来,在这种情况下,callback 按预期位于LinkExtractor 括号内。

任何想法是什么导致了这个错误?

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    是的,callback 肯定会传递给LinkExtractor。实际上,这似乎是问题所在,因为我在the documentation 中的该类的预期参数下看不到callback

    我看到Rule确实 在文档中列出了一个回调参数。那么也许您应该将它传递给 Rule 而不是 LinkExtractor?

    Rule(LinkExtractor(allow=r'.*huis.*'), callback='parse_item')
    

    如果您在想“但是为什么链接问题的回答者将 callback 放在 LinkExtractor 调用中?”,我认为您可能误解了括号的嵌套,这确实有点令人困惑。更改布局使其更清晰:

    rules = (
        Rule(
            LinkExtractor(
                allow=[r'/*'], 
                deny=('blogs/*', 'videos/*', )
            ),
            callback='parse_html'
        ), 
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2017-05-10
      • 1970-01-01
      • 2018-08-01
      • 2017-10-16
      • 2020-10-22
      • 2021-11-17
      相关资源
      最近更新 更多