使用BeautifulSoup4删除所有HTML标记(Python3.4)

2024-04-20 02:04:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解决这个问题已经有一段时间了,但是我成功做到这一点的唯一方法是使用一个复杂的while循环。在

我想输入以下内容:

"<td colspan='2' class='ToEx'>This is a test (<i> to see </i> this works) and I really hope it does</td>"

然后输出这个:

^{pr2}$

本质上,我想删除带有“< gt;”的所有内容以及介于两者之间的任何内容。我能做的最好的几个命令是:

^{3}$

但我只剩下这些讨厌的家伙:<i></i>

这是我的代码:

from bs4 import BeautifulSoup

text = "<td colspan='2' class='ToEx'>This is a test (<i> to see </i> this works) and I really hope it does</td>" 
soup = BeautifulSoup(text)
content = soup.find_all("td","ToEx")
content[0].renderContents()

Tags: andtotestisitthisclasstd
2条回答

只需打印标记的.text属性,就可以得到它的文本

print(content[0].text)

输出:

^{pr2}$

我会使用get_text()-它是为这种情况而设计的:

text = "<td colspan='2' class='ToEx'>This is a test (<i> to see </i> this works) and I really hope it does</td>" 
soup = BeautifulSoup(text)
print(soup.get_text())

这应该可以工作as per the documentation。在

我以前从未见过.text使用过,相反,在Beautiful Soup 4中,请使用.string-如果你想用的话:

^{pr2}$

两者都将输出:

This is a test ( to see this works) and I really hope it does

这两种方法都可以很好地工作,但是get_text()将更容易使用,特别是如果您想将文本保存到变量等

相关问题 更多 >