首页 > 解决方案 > Python BeautifulSoup - 从 p 获取值

问题描述

html = '<p class="product-new-price">96<sup>33</sup> <span class="tether-target tether-enabled tether-element-attached-top tether-element-attached-left tether-target-attached-top tether-target-attached-right">Lei</span>
</p>'

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

sup_elem = soup.find("sup").string # 33 - it works

如何在元素之前获得“96”?

标签: pythonhtmlparsingbeautifulsoup

解决方案


请改用选择。

from bs4 import BeautifulSoup

html = '''<p class="product-new-price">96<sup>33</sup> <span class="tether-target tether-enabled tether-element-attached-top tether-element-attached-left tether-target-attached-top tether-target-attached-right">Lei</span>
</p>'''

soup = BeautifulSoup(html, 'html.parser')
print(soup.select_one('.product-new-price').text.strip().replace('Lei',''))

没有“。” 在源代码中,但您始终可以除以 100

print(int(soup.select_one('.product-new-price').text.strip().replace('Lei',''))/100)

推荐阅读