我试图解决这个问题已经有一段时间了,但是我成功做到这一点的唯一方法是使用一个复杂的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()
只需打印标记的
.text
属性,就可以得到它的文本输出:
^{pr2}$我会使用
get_text()
-它是为这种情况而设计的:这应该可以工作as per the documentation。在
我以前从未见过
^{pr2}$.text
使用过,相反,在Beautiful Soup 4中,请使用.string
-如果你想用的话:两者都将输出:
这两种方法都可以很好地工作,但是
get_text()
将更容易使用,特别是如果您想将文本保存到变量等相关问题 更多 >
编程相关推荐