- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要替换 html 文档中的多个单词。 Atm 我通过为每次替换调用一次 replace_with 来做到这一点。在 NavigableString 上调用 replace_with 两次会导致 ValueError(见下面的例子),因为被替换的元素不再在树中。
最小的例子
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import re
def test1():
html = \
'''
Identify
'''
soup = BeautifulSoup(html,features="html.parser")
for txt in soup.findAll(text=True):
if re.search('identify',txt,re.I) and txt.parent.name != 'a':
newtext = re.sub('identify', '<a href="test.html"> test </a>', txt.lower())
txt.replace_with(BeautifulSoup(newtext, features="html.parser"))
txt.replace_with(BeautifulSoup(newtext, features="html.parser"))
# I called it twice here to make the code as small as possible.
# Usually it would be a different newtext ..
# which was created using the replaced txt looking for a different word to replace.
return soup
print(test1())
预期结果:
The txt is == newstring
结果:
ValueError: Cannot replace one element with another when the element to be replaced is not
part of the tree.
一个简单的解决方案就是修改新字符串,最后只替换一次,但我想了解当前的现象。
最佳答案
第一txt.replace_with(...)
删除 NavigableString
(此处存储在来自文档树( doc )的变量 txt
中)。这有效地设置了 txt.parent
至 None
第二个txt.replace_with(...)
看着 parent
属性,找到 None
(因为 txt
已经从树中移除)并抛出 ValueError。
正如您在问题末尾所说,解决方案之一是使用 .replace_with()
只有一次:
import re
from bs4 import BeautifulSoup
def test1():
html = \
'''
word1 word2 word3 word4
'''
soup = BeautifulSoup(html,features="html.parser")
to_delete = []
for txt in soup.findAll(text=True):
if re.search('word1', txt, flags=re.I) and txt.parent.name != 'a':
newtext = re.sub('word1', '<a href="test.html"> test1 </a>', txt.lower())
# ...some computations
newtext = re.sub('word3', '<a href="test.html"> test2 </a>', newtext)
# ...some more computations
# and at the end, replce txt only once:
txt.replace_with(BeautifulSoup(newtext, features="html.parser"))
return soup
print(test1())
打印:
<a href="test.html"> test1 </a> word2 <a href="test.html"> test2 </a> word4
关于python - BS4 replace_with 结果不再在树中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63424180/
我正在尝试运行基于 angular2 的第一个应用程序。我关注了angular2脚步。当我运行命令 npm start 然后它在终端中给出以下错误 Did not detect a bs-config
×
我在 stackovefflow 上搜索并没有找到这个问题的答案。我是 Angular 2 的新手,我正在创建一个演示应用程序,但是当我执行 npm start 时,此消息显示在终端/控制台上。虽然它
如果 .popover() 之后变成 .on('hidden.bs.popover'),我想'销毁'是 .on('shown.bs.popover' 所以它不会再 'show' 。一切似乎都正确,但看
我在玩 vimgolf challenge 时遇到了以下问题: :%s/V/i?/giiZZ 我认为意思是“退格键”,但是当我在键盘的命令行模式下按退格键时,它只是删除了“?” (我正在使用带有 Mi
我正在制作一个 BS 轮播。当浏览器较小且文本未居中时,图像看起来不合适并且不会完全覆盖轮播。我该如何解决这个问题?我还没有找到可以帮助我的答案。 这是我的 CSS: .carousel-inn
在优势网格中,当单击另一个分区时,一个分区的卡体会折叠。然而,另一个网格并没有发生同样的情况(缺点)。当我们单击劣势部分的一个分区时,当我们再次单击劣势部分的另一个分区时,它不会被折叠。然而,这是在优
在下面的代码中,我正在学习如何创建 2 列,并将左列用作可滚动的导航栏。我这里还处于零阶段,所以请原谅质量! 我在将 .sidebar 类设置为左栏中的 "position:fixed" 时遇到了问题
我尝试使用 javascript 动态 + bs + python 进行网页抓取,并且我阅读了很多内容来编写此代码,例如,我尝试在著名网站上抓取使用 javascript 呈现的价格: from bs
我想在右侧的标题中添加一个额外的小元素,该元素与右侧对齐,如下所示: Headersubheadersubheader right 但是右侧的 span 元素放置得更高/与第一个小元素不在一条直线上。
我正在使用 BS3 并且有这样的选项卡: Annual Summary Monthly Summary 我在第一个选项卡中有一个像这样的链接:
有谁知道如何在使用 css 打开或关闭时更改 bootstrap 4 navbar-toggler-icon?我认为这很简单,但我真的找不到任何东西。 .map-controls-mobile .na
大家好……我想从网页上的表格中选择一个关于特定位置的词。源代码如下: table = ''' Code: BAN Color: White ''
我正在尝试使用现有代码库配置 gcov 以实现代码覆盖。现有代码base 是基于服务的代码库(即包含多个文件 c 和 cpp 以及一项服务) 我已经关注了配置 gcov 的 wiki 页面并添加了 -
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
我在 FlashBuilder 中开发了一个浏览器部署的全文搜索应用程序,该应用程序以 REST 方式与远程 Web 服务器进行通信。该软件适合一个小众市场——它适用于古代语言而不是现代语言,我不可能
我正在尝试使用 Angular 带的 Bootstrap 工具提示编写简单的代码。我发现使用 bs-tooltip 时 AngularJs 数据绑定(bind)不起作用。 绑定(bind)不起作用
我使用 bootstrap modal + slick slider 在 bs modal 窗口中显示 slider ,但在显示时遇到一些问题。自动播放之前 slider 未打开 not work 但
我正在尝试从引导下拉列表中捕获事件。似乎什么都不起作用,尽管显然我做错了什么。 代码,精简为问题...... Dem
为什么 loaded.bs.modal 事件从 Bootstrap 4 中删除?有什么东西可以代替它吗? shown.bs.modal 非常相似,但它不会等待模态完全加载,而只是显示。 最佳答案 基本
我是一名优秀的程序员,十分优秀!