【发布时间】:2013-03-16 21:55:30
【问题描述】:
请考虑以下 python 会话:
>>> from BeautifulSoup import BeautifulSoup
>>> s = BeautifulSoup("<p>This <i>is</i> a <i>test</i>.</p>"); myi = s.find("i")
>>> myi.replaceWith(BeautifulSoup("was"))
>>> s.find("i")
>>> s = BeautifulSoup("<p>This <i>is</i> a <i>test</i>.</p>"); myi = s.find("i")
>>> myi.replaceWith("was")
>>> s.find("i")
<i>test</i>
请注意第 4 行后 s.find("i") 的缺失输出!
这是什么原因?有解决办法吗?
编辑:实际上,该示例并未演示用例,即:
myi.replaceWith(BeautifulSoup("wa<b>s</b>"))
每当插入的部分包含自己重要的 html 代码时,我看不出如何用其他内容替换此语法。只是有
myi.replaceWith("wa<b>s</b>")
将用实体替换 html 特殊字符。
【问题讨论】:
-
为什么要替换成
sometag.renderContents()而不是直接替换成someTag? -
好的,让我们通过添加一个不同的示例来更具体...(见上文,我再次编辑)
标签: python find beautifulsoup