使用Beautiful Soup查找XML中的所有属性

时间:2014-08-23 20:51:13

标签: python xml beautifulsoup

我有一个XML文件,如下所示:

<tagA key1="val1" key2="val2" key3="val3">
<tagB.1 key1="val1" key2="val2" key3="val3"/>
<tagB.2 key1="val1" key2="val2" key3="val3"/>
<tagB.3 key1="val1" key2="val2" key3="val3"/>
<tagB.4 key1="val1" key2="val2" key3="val3"/>
<tagB.5 key1="val1" key2="val2" key3="val3"/>
</tagA>

我要做的是在key1中提取key2key3tagB.x的名称,并将它们放入列表中。这样我以后可以提取它的值。它应该能够处理更多或更少的元素,因为每个文件都是不同的。谢谢!

1 个答案:

答案 0 :(得分:2)

您应该使用xml解析器:

xml="""
<tagA key1="val1" key2="val2" key3="val3">
<tagB.1 key1="val1" key2="val2" key3="val3"/>
<tagB.2 key1="val1" key2="val2" key3="val3"/>
<tagB.3 key1="val1" key2="val2" key3="val3"/>
<tagB.4 key1="val1" key2="val2" key3="val3"/>
<tagB.5 key1="val1" key2="val2" key3="val3"/>
</tagA>
"""


import xml.etree.ElementTree as ET

root = ET.fromstring(xml)
for child in root:
    print child.tag, child.attrib.keys()

tagB.1 ['key3', 'key2', 'key1']
tagB.2 ['key3', 'key2', 'key1']
tagB.3 ['key3', 'key2', 'key1']
tagB.4 ['key3', 'key2', 'key1']
tagB.5 ['key3', 'key2', 'key1']