Skip to main content
 首页 » 编程设计

python之需要帮助在 python 中使用正则表达式解析 HTML

2025年05月04日24mfrbuaa

我的字符串是

mystring = "<tr><td><span class='para'><b>Total Amount : </b>INR (Indian Rupees)  
100.00</span></td></tr>" 

我的问题是我必须搜索并获得总金额

test = re.search("(Indian Rupees)(\d{2})(?:\D|$)", mystring) 

但是我的测试没有给我。 我怎样才能得到值,值可以是 10.00、100.00、1000.00

谢谢

请您参考如下方法:

我强烈建议为此使用真正的 HTML 解析器,而不是自定义正则表达式。

这是一个关于 BeautifulSoup 的例子图书馆:

from BeautifulSoup import BeautifulSoup 
 
str = r''' 
<tr><td><span class='para'><b>Total Amount : </b>INR (Indian Rupees) 100.00</span></td></tr> 
''' 
 
soup = BeautifulSoup(str) 
 
amount = soup.findAll('span', attrs={'class': 'para'}) 
amount_tokens = amount[0].text.split() 
print amount_tokens[-1]