【发布时间】:2020-04-24 18:21:39
【问题描述】:
假设我有项目div,div 是一个 beautifulsoup 对象(由 findAll 获得)。源代码如下:
<div>text1 <span>text2</span></div>
我想要做的是用 text3 替换 text1。我试过了:
div.string.replace_with(newstr), where newstr="text3 <span>text2</span>"这不起作用,因为 div.string 是 Nonediv.replace_with(newstr)
这不起作用,因为当我将 html 代码保存到文件中时,最终结果显示&lt和&gt而不是“”。
【问题讨论】:
-
您使用的是哪个解析器?尝试使用 lxml
-
你的意思是初始化时Beautifulsoup(...,features='lxml')中的特征属性。我已经这样做了。
标签: python beautifulsoup