python标签的Scrapy提取值

时间:2018-09-02 22:39:21

标签: python scrapy

我是Scrapy的新手,我正在尝试抓取在类上带有aria标签的页面:

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

2 个答案:

答案 0 :(得分:1)

您的代码中有几个错误:

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

答案 1 :(得分:1)

如果要使用CSS提取器代替xpath:

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