我使用beautifulsoup时无法更改字符串。字符串是自动编码

时间:2017-06-17 15:25:25

标签: python beautifulsoup

<>是自动编码。

请帮助我,谢谢。

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用Tag.new_tag(..)Tag.clear()Tag.append()更具语义的方式执行此操作:

atag = soup.a
atag.clear() # clear the content of the <a> tag
atag.append('support') # add the 'support' text
emtag = soup.new_tag('em') # create a new <em> tag
atag.append(emtag) # add the <em> tag to the <a> tag
emtag.string = '[6666]' # alter the text in the <em> tag

这构造:

>>> soup
<html><body><a href="#" onclick="foo">support<em>[6666]</em></a></body></html>

在我的机器上。