简体   繁体   中英

Remove special characters in scrapy xpath

Cannot seem to accomplish after many hours. I am trying to edit a script.

Price from scrapy = $123,456 I need 123456 instead.

I have tried this but get attribute errors and more.

price_txt = response.xpath(".//dt[contains(text(), 'List Price')]/following-sibling::dd/text()").extract_first()


price = price_txt.translate(str.maketrans('', '', '.,$()'))

Use.replace()

price_txt = response.xpath(".//dt[contains(text(), 'List Price')]/following-sibling::dd/text()").extract_first()


price = price_txt.replace('$', '').replace(',', '')

Using Regex.

Ex:

import re

price_txt = "$123,456"
print(re.sub(r"[^\d]", "", price_txt))

Or str.isdigit

Ex:

print("".join(i for i in price_txt if i.isdigit()))

Output:

123456
123456

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM