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時刪除字符的適當方法?
感謝這個!無論如何,我可以用'extract()'來使用它嗎? – Maverick
@Maverick當然,'.re()'在這種情況下實際上充當「extract()」替換。你的具體用例是什麼? – alecxe
啊我明白了。我有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