Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-如何从bs4输出中提取数字_Python_Beautifulsoup - Fatal编程技术网

Python-如何从bs4输出中提取数字

Python-如何从bs4输出中提取数字,python,beautifulsoup,Python,Beautifulsoup,我正试图从使用BeautifulSoup的网站上获取价格,到目前为止,我已成功获得: <h2>£<!-- -->199.99</h2> 199.99英镑 我只想收到“199.99英镑” 有没有办法过滤掉这些字母 提前感谢重新使用 import re s = "<h2>£<!-- -->199.99</h2>" rx_price = re.compile(r'([0-9.]+)') content = re.sub(

我正试图从使用BeautifulSoup的网站上获取价格,到目前为止,我已成功获得:

<h2>£<!-- -->199.99</h2>
199.99英镑
我只想收到“199.99英镑” 有没有办法过滤掉这些字母

提前感谢

重新使用

import re

s = "<h2>£<!-- -->199.99</h2>"

rx_price = re.compile(r'([0-9.]+)')

content = re.sub(r'<.+?>', '', s)

print (f"£{rx_price.findall(content)[0]}")

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

从bs4导入美化组
html='199.99英镑'
soup=BeautifulSoup(html,'html5lib')
结果=soup.find('h2')。获取文本(strip=True)
打印(结果)
#£199.99

您的意思是想要文本内容吗?你读过BS4文档了吗?你的代码在哪里?请仔细阅读并提出一个更容易理解的问题。
print(soup.find(“h2”).text)
你根本不需要
strip
元素!你处理的是
CDATA
,它在源代码中被无声地剥离。坏习惯,在我这方面,如果
OP
正在使用
bs4
和标记
bs4
,而这可以在
bs4
中完成,为什么他需要使用
REGEX?
£199.99