2016-05-11 188 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