繁体   English   中英

python 中 beautifulsoup 的逃逸问题

[英]escape problem with beautifulsoup in python

我在 python 中使用 beautifulsoup4,以便在使用 soup.append(str) function 时更新汇合页面上的表格, < >被转义并变为&lt; &gt; &lt; &gt; 这样我就无法正确更新表格。 有人可以给我提示吗? 或者也许是一些更好的解决方案来更新汇合页面上的表格,在此先感谢。 :)

我的期望:

<tr>"string"</tr>

结果是什么:

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

发生这种情况是因为您将 append 字符串连接到其他标签。 解决方案是创建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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM