python 中 beautifulsoup 的转义问题

escape problem with beautifulsoup in python

我在 python 中使用 beautifulsoup4 以便在我使用 soup.append(str) 函数时更新合流页面上的 table, < > 被转义并变成了 &lt; &gt;,所以我无法正确更新 table。有人可以给我提示吗?或者可能有一些更好的解决方案来更新 confluence 页面上的 table,在此先感谢。 :)

我的期望:

<tr>"string"</tr>

结果如何:

&lt;tr &gt;"string"&lt; tr&gt;

发生这种情况是因为您将字符串附加到其他标签。解决方案是创建 new_tag() 或全新的汤。

例如:

txt = '''
<div>To this tag I append other tag</div>
'''

soup = BeautifulSoup(txt, 'html.parser')
soup.find('div').append( BeautifulSoup('<tr>string</tr>', 'html.parser') )

print(soup.prettify())

打印:

<div>
 To this tag I append other tag
 <tr>
  string
 </tr>
</div>