简体   繁体   English

删除 scrapy xpath 中的特殊字符

[英]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.价格从scrapy = $123,456我需要 123456 代替。

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()使用.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.isdigitstr.isdigit

Ex:前任:

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

Output: Output:

123456
123456

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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