使用BeautifulSoup删除xml解析中包含特定子标签的标签

时间:2018-05-01 09:34:02

标签: python xml beautifulsoup

我正在尝试使用Python中的BeautifulSoup解析XML文件。 XML文件如下:

<x id = '123'>
<b><c>abcd</c>
</b>
</x>
<x id ='456'><z></z>
</x>
<x id ='567'><c>def</c>
</x>

如果任何外部标记<x>包含子标记<c>,我想删除整个标记。我该怎么做Python?

1 个答案:

答案 0 :(得分:0)

首先,使用self查找所有<div ng-repeat="s in legendItems"> <div class="epLegendeColorBoxPopover" ng-style='{"background-color": s.color}'></div><input type="text" ng-model="s.description"> </div> 代码。然后,如果标记包含x标记(可以使用find_all('x')进行检查),请使用decompose()删除标记。

<c>

输出:

if x.find('c'):
相关问题