一.find:根据节点名称获取第一个指定节点
1.根据名称获取第一个子节点,并打印子节点的属性
# --------假设获取了根节点-------
from xml.etree import ElementTree as ET
# 下列是文本字符串
content = """
<data name='123'>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
# 直接从文本字符串,获取根节点
root = ET.XML(content)
# --------假设获取了根节点-------
# -------这里使用find,根据country名称,获取country对象--------
country_obj = root.find('country')
"""这里需要注意的是:获取的只是第一个country节点,其余的获取不了"""
# country_obj的三种属性,可以打印出来
print(country_obj.tag,country_obj.attrib,country_obj.text)
# 输出是:country {'name':'Liechtenstein'}
2.获取第一个子节点属性后,获取子节点的子节点
from xml.etree import ElementTree as ET
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
root = ET.XML(content)
v1 = root.find('country').find('rank')
print(v1.text)
但是find只能获取一个节点,如果节点重名,其他节点想获取的话怎么办呢?
二.findall:获取所有这个名称的子节点
root.findall(‘节点名称’)会把root下所有这个名称的节点对象抓出来,返回类型是一个列表.
这个有什么用呢?
# 需求:把下面content里根标签下第二个country节点 的属性打印出来
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
这个时候用findall打印就非常方便了
from xml.etree import ElementTree as ET
content = """
<data>
<country name="Liechtenstein">
<rank>2</rank>
<year>2023</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Panama">
<rank>69</rank>
<year>2026</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
"""
root = ET.XML(content)
print(root.findall('country')[1].attrib)