【问题标题】:Scrapy error when following links: AttributeError: 'HtmlResponse' object has no attribute 'follow_all'跟踪链接时出现 Scrapy 错误:AttributeError:“HtmlResponse”对象没有属性“follow_all”
【发布时间】:2020-03-05 16:56:04
【问题描述】:

我是一名 python 和 scrapy 初学者,目前正试图在 https://www.twitchmetrics.net/channels/viewership 上获得每种语言/游戏组合的排名

但是,我无法按照链接进行操作。我总是得到一个 'HtmlResponse' 对象没有属性 'follow_all' - 错误。

    def parse(self, response):
    all_channels = response.xpath('//h5')
    language_page_links = response.xpath(
        '//div[@class="mb-4"][1]//a//@href').getall()

    for i, channel in enumerate(all_channels, start=1):
        il = ItemLoader(item=LeaderboardItem(), selector=channel)
        il.add_xpath('channel_id', './text()')
        il.add_value('rank_mostwatched_all_all', i)
        yield il.load_item()

    yield from response.follow_all(language_page_links, self.parse)

在最后一行中,一旦链接跟踪工作正常,我将使用不同的解析器。我还尝试了来自 scrapy 文档的示例刮板,我得到了完全相同的错误:

class AuthorSpider(scrapy.Spider):
name = 'author'

start_urls = ['http://quotes.toscrape.com/']

def parse(self, response):
    author_page_links = response.css('.author + a')
    yield from response.follow_all(author_page_links, self.parse_author)

    pagination_links = response.css('li.next a')
    yield from response.follow_all(pagination_links, self.parse)

def parse_author(self, response):
    def extract_with_css(query):
        return response.css(query).get(default='').strip()

    yield {
        'name': extract_with_css('h3.author-title::text'),
        'birthdate': extract_with_css('.author-born-date::text'),
        'bio': extract_with_css('.author-description::text'),
    }

我在这里错过了什么?

【问题讨论】:

  • 总是将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图)放在有问题的(不是评论)中。还有其他有用的信息。

标签: python web-scraping scrapy web-crawler


【解决方案1】:

文档显示follow_all 是仅在 2.0 版中可用的新方法。

您可能需要更新scrapy

 pip install --update scrapy

【讨论】:

  • 就是这样,非常感谢。最初我没有使用 conda-forge 来安装 scrapy,最终在不知不觉中安装了 1.6 版
猜你喜欢
  • 2015-09-17
  • 2019-11-15
  • 2021-10-12
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多