【问题标题】:Python Scrapy Extract Value of aria-labelaria-label 的 Python Scrapy 提取值
【发布时间】:2018-09-03 17:36:14
【问题描述】:

我是 Scrapy 的新手,我正在尝试抓取一个在类上有 aria-label 的页面:

<body>
  <div class="item-price" aria-label="$1.99">
    .....
  </div>
</body>

我正在尝试在我的蜘蛛上使用以下解析来提取标签:

def parse(self, response):
   price = circular_item.css("div.item-price > aria-label::text").extract()
   yield price

当我运行蜘蛛时,我收到以下错误:

2018-09-02 18:34:03 [scrapy.core.scraper] ERROR: Spider must return Request, BaseItem, dict or None, got 'list' in <GET https://example.com/test.html>

如何在这里提取 aria-label 的值?

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    您的代码中有几个错误:

    def parse(self, response):
       item = {}
       item["price"] = response.xpath('//div[@class="item-price"]/@aria-label').extract_first()
       yield item
    

    【讨论】:

      【解决方案2】:

      如果你想使用 css 提取器而不是 xpath:

      def parse(self, response):
          item = {response.css('div.item-price::attr(aria-label)').extract_first()}
          yield item
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-04
        • 1970-01-01
        • 2023-03-10
        • 2017-07-29
        • 1970-01-01
        • 2023-01-17
        • 2013-11-06
        相关资源
        最近更新 更多