import bs4 import BeautifulSoup as bs
h="<b>b-text</b>"
s=bs(h,'html5lib')
b_s=s.b.string
html_code="<a>a-string</a>"
b_s.replace_with(html_code)
print b
## <b><a>a-string</a></b>
如上代码可以顺利执行,只是,html_code 里边的尖括号都被 bs 转了. 那怎么样才能让 bs 不要转义我插入的字符串呢? 多谢您的回复!