python - 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”?
解决方案
请改用选择。
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)
推荐阅读
- python - 如何反转编码算法
- java - 是什么导致此方法中出现此异常?java.lang.OutOfMemoryError:Java 堆空间
- c# - 在 Main 函数以外的任何类中使用 Selenium IWebDriver 从 driver.Url 获取“System.NullReferenceException”。C#
- android - android - android:name 和 android:label 之间的区别
- python - Django 中的 sqlalchemy.exc.ProgrammingError
- php - 为什么 Travis CI 无法测试我的 Laravel 网络路由?
- r - 遍历列并返回值
- symfony - Symfony 5 和 API 平台,在验证日期时间时总是收到“这个值应该是字符串类型”
- html - 我如何将按钮和图像对齐?
- javascript - 尽管有异步包装器,但等待 Promise.all 不等待