gpt4 book ai didi

python - 替换 Scrapy 项目中的字符

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:39 25 4
gpt4 key购买 nike

我正在尝试使用 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'

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

最佳答案

extract() 会返回一个列表,您可以使用extract_first() 来获取单个值:

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

或者,您可以使用 .re() method ,类似于:

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

关于python - 替换 Scrapy 项目中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37164636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com