首页 > 解决方案 > aria-label 的 Python Scrapy 提取值

问题描述

我是 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 的值?

标签: pythonscrapy

解决方案


您的代码中有几个错误:

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

推荐阅读