![](/img/trans.png)
[英]Scrapy xpath not extracting div containing special characters <%=
[英]Remove special characters in scrapy xpath
幾個小時后似乎無法完成。 我正在嘗試編輯腳本。
價格從scrapy = $123,456
我需要 123456 代替。
我已經嘗試過了,但是得到了屬性錯誤等等。
price_txt = response.xpath(".//dt[contains(text(), 'List Price')]/following-sibling::dd/text()").extract_first()
price = price_txt.translate(str.maketrans('', '', '.,$()'))
使用.replace()
price_txt = response.xpath(".//dt[contains(text(), 'List Price')]/following-sibling::dd/text()").extract_first()
price = price_txt.replace('$', '').replace(',', '')
使用正則表達式。
前任:
import re
price_txt = "$123,456"
print(re.sub(r"[^\d]", "", price_txt))
或str.isdigit
前任:
print("".join(i for i in price_txt if i.isdigit()))
Output:
123456
123456
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.