find() 后 replaceWith() 不起作用(使用 BeautifulSoup) [英] find() after replaceWith() doesn't work (using BeautifulSoup)
问题描述
请考虑以下 python 会话:
<预><代码>>>>从 BeautifulSoup 导入 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("是")>>>s.find("i")<i>测试</i>请注意第 4 行后 s.find("i") 的缺失输出!
这是什么原因?有解决方法吗?
实际上,该示例并未演示用例,即:
myi.replaceWith(BeautifulSoup("was"))
每当插入的部分包含自己重要的 html 代码时,我不知道您如何用其他东西替换此语法.刚好
myi.replaceWith("was")
将用实体替换 html 特殊字符.
更简单的答案:调用 replaceWith
后,通过调用 s 重新生成和清理
.然后你可以再次s
= BeautifulSoup(s.renderContents())find
.
Please consider the following python session:
>>> 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>
Please note the missing output of s.find("i") after line 4!
What's the reason for this? Is there a workaround?
EDIT: Actually, the example doesn't demonstrate the usecase, which is:
myi.replaceWith(BeautifulSoup("wa<b>s</b>"))
Whenever the inserted part contains itself nontrivial html code, I don't see how you could replace this syntax with something else. Just having
myi.replaceWith("wa<b>s</b>")
will replace the html special chars by entities.
Simpler answer : after your call to replaceWith
, regenerate and clean s
by calling s = BeautifulSoup(s.renderContents())
. Then you can find
again.
这篇关于find() 后 replaceWith() 不起作用(使用 BeautifulSoup)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!