python – 在replaceWith()不起作用后的find()(使用BeautifulSou
发布时间:2020-12-16 23:02:26 所属栏目:Python 来源:网络整理
导读:请考虑以下 python会话: from BeautifulSoup import BeautifulSoup s = BeautifulSoup("pThis iis/i a itest/i./p"); myi = s.find("i") myi.replaceWith(BeautifulSoup("was")) s.find("i") s = BeautifulSoup("pThis iis/i a itest/i./p"); myi = s.find(
请考虑以下
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”)的缺失输出! 这是什么原因?有解决方法吗? 编辑:实际上,该示例未演示usecase,它是: myi.replaceWith(BeautifulSoup("wa<b>s</b>")) 每当插入的部分包含非常重要的html代码时,我都不会看到如何用其他东西替换这种语法.只是拥有 myi.replaceWith("wa<b>s</b>") 将替换实体的html特殊字符. 解决方法
更简单的答案:在你调用replaceWith之后,通过调用s = BeautifulSoup(s.renderContents())来重新生成和清理s.然后你可以找到.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |