替换Scrapy项目中的字符

ric*_*row 3 python scrapy web-scraping

我正试图从使用Scrapy的商业网站上刮掉.对于价格标签,我想删除"$",但我当前的代码不起作用.

  def parse(self, response):
    for sel in response.xpath('//section[@class="items-box"]'):
      item = ShopItem()
      item['name'] = sel.xpath('a/div/h3/text()').extract()
      item['price'] = sel.xpath('a/div/div/div[1]/text()').extract().replace("$", "")
      yield item

AttributeError: 'list' object has no attribute 'replace'
Run Code Online (Sandbox Code Playgroud)

使用Scrapy时删除字符的适当方法是什么?

ale*_*cxe 6

extract()会返回一个列表,你可以extract_first()用来获得一个值:

item['price'] = sel.xpath('a/div/div/div[1]/text()').extract_first().replace("$", "")
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用以下.re()方法:

item['price'] = sel.xpath('a/div/div/div[1]/text()').re(r"\$(.*?)")
Run Code Online (Sandbox Code Playgroud)