简体   繁体   English

python标签的Scrapy提取值

[英]Python Scrapy Extract Value of aria-label

I'm new to Scrapy and I'm trying to scrape a page that has an aria-label on a class: 我是Scrapy的新手,我正在尝试抓取在类上具有aria标签的页面:

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

I'm trying to extract the label with the following parse on my spider: 我正在尝试对蜘蛛进行以下解析来提取标签:

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

When I run the spider I get the following error: 当我运行蜘蛛时,出现以下错误:

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>

How do I extract the value of aria-label here? 如何在此处提取aria标签的价值?

You have several errors in your code: 您的代码中有几个错误:

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

If you want to use a css extractor instead of xpath: 如果要使用css提取器而不是xpath:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM