【发布时间】:2021-03-29 23:05:50
【问题描述】:
如何删除 beautifulsoup 对象中的“冗余”html 标签?
以
为例<html>
<body>
<div>
<div>
<div>
<div>
<div>
<div>
Close
</div>
</div>
</div>
</div>
</div>
<div>
<div>
<div style="width:80px">
<div>
</div>
<div>
<button>
Close
</button>
</div>
</div>
</div>
</div>
</div>
<div>
</div>
</body>
</html>
如何将多余的<div>标签(冗余,因为它们只增加深度,但不包含任何附加信息或属性)删除到以下结构:
<html>
<body>
<div>
Close
</div>
<div style="width:80px">
<button>
Close
</button>
</div>
</body>
</html>
就图形算法而言,我正在尝试将 beautifulsoup 树中的多个节点合并在一起,这些节点不包含字符串,也不包含属性。
【问题讨论】:
-
只是为了确保答案不太适合示例 html:您有一个带有文本 (
<button>) 的元素,其中一个祖先具有属性,而另一个元素没有这样的祖先。但是文本元素有可能有两个具有属性的嵌套祖先吗?具有属性的元素是否可能没有带有文本的子元素? -
是的,最好有一个尽可能通用的 sn-p :)
标签: python html beautifulsoup tree xml-parsing