我正在尝试使用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时删除字符的适当方法是什么?
答案 0 :(得分:5)
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"\$(.*?)")
答案 1 :(得分:0)
您可以先使用 join()
,然后使用 replace()
将列表转换为字符串并将 "$"
替换为 ""
示例:
prices = ''.join(price)
if '$' in prices:
prices = prices.replace("$", "")
希望对您有所帮助...