gpt4 book ai didi

python - 在 div 中刮取 2 个内部文本作为一个值

转载 作者:太空宇宙 更新时间:2023-11-04 04:39:27 25 4
gpt4 key购买 nike

我有以下html

<div class="price-block__highlight"><span class="promo-price" data- 
test="price">102,
<sup class="promo-price__fraction" data-test="price-fraction">99</sup>
</span>

</div>

我想在没有逗号的情况下打印这个 html 的价格,所以打印价格应导致:102.99

我有以下代码

pricea = page_soup.find("div", {"class":"price-block__highlight"})
price = str(pricea.text.replace('-','').replace(',','.').strip())
print price

然而,这会导致:

 102.
99

当写入 csv 时,它会创建多行。如何将两个数字都放在一个值中?

最佳答案

我认为你正在使用 bs4

from bs4 import BeautifulSoup

html_doc = """
<div class="price-block__highlight"><span class="promo-price" data-
test="price">102,
<sup class="promo-price__fraction" data-test="price-fraction">99</sup>
</span>

</div>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

price_div = soup.find("div", {"class": 'price-block__highlight'})

texts = [x.strip() for x in price_div.text.split(',')]

print('.'.join(texts))

输出

102.99

关于python - 在 div 中刮取 2 个内部文本作为一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50998990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com