首页 > 解决方案 > Python - 如何从 bs4 输出中提取数字

问题描述

我正在尝试使用 BeautifulSoup 从网站上获取价格,到目前为止,我已经设法获得:

<h2>£<!-- -->199.99</h2>

我只想收到“199.99 英镑”有没有办法过滤掉这些字母?

提前致谢

标签: pythonbeautifulsoup

解决方案


get_text如有必要,您将使用带有 strip=True 的函数进行清理

from bs4 import BeautifulSoup


html = '<h2>£<!-- -->199.99</h2>'
soup = BeautifulSoup(html,'html5lib')

result = soup.find('h2').get_text(strip=True)

print(result)
#£199.99


推荐阅读