我正在尝试清理我通过网络抓取提取的部分数据。包含数据的 HTML 代码如下:
<li class="price-was">
$1,699.00
<span class="price-was-data" style="display: none">1699.00</span>
</li>
要提取数据,我使用以下代码行:
price_products_before = product.findAll("li",{"class":"price-was"})
PriceBefore = price_products_before[0].text
我使用这个是因为数据是这样的:
'\r\n $1,699.00\r\n 1699.00\n'
使用下面的代码行,我设法以某种方式清理了它,但我仍然有两倍的数字。
PriceBefore = price_products_before[0].text.strip().replace("\r\n","")
我只需要一次 1699 没有任何空格 \r 或 \n。
from bs4 import BeautifulSoup
html = """<li class="price-was">
$1,699.00
<span class="price-was-data" style="display: none">1699.00</span>
</li>"""
soup = BeautifulSoup(html, 'html.parser')
try:
print(soup.find("li", class_="price-was").next_element.strip())
except:
print("Not Found")
输出:
$1,699.00
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句