2016-05-11 161 views
2

我试图从使用Scrapy的商业网站上刮擦。对于价格标签,我想删除“$”,但我目前的代码不起作用。替换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时删除字符的适当方法?

回答

4

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"\$(.*?)") 
+0

感谢这个!无论如何,我可以用'extract()'来使用它吗? – Maverick

+1

@Maverick当然,'.re()'在这种情况下实际上充当“extract()”替换。你的具体用例是什么? – alecxe

+0

啊我明白了。我有3我目前正在处理:1)'response.xpath('/ html/body/div [2]/div [1]/section [2]/div/form/div [3]/div [2] 2)'response.xpath('/ html/body/div [2]/div [1]/section [/ p/text()')。extract_first()。replace(“per week”,“”) 2]/div/form/div [3]/div [1]/label/text()')。extract_first()。replace(“\ n”,“”)' 3)'response.xpath('/ html/body/div [2]/div [1]/section [2]/div/form/div [2]/div [2]/p/text()')。extract_first()。replace - “,”“)' – Maverick