一.find:根据节点名称获取第一个指定节点

1.根据名称获取第一个子节点,并打印子节点的属性

  1. # --------假设获取了根节点-------
  2. from xml.etree import ElementTree as ET
  3. # 下列是文本字符串
  4. content = """
  5. <data name='123'>
  6. <country name="Liechtenstein">
  7. <rank updated="yes">2</rank>
  8. <year>2023</year>
  9. <gdppc>141100</gdppc>
  10. <neighbor direction="E" name="Austria" />
  11. <neighbor direction="W" name="Switzerland" />
  12. </country>
  13. <country name="Panama">
  14. <rank updated="yes">69</rank>
  15. <year>2026</year>
  16. <gdppc>13600</gdppc>
  17. <neighbor direction="W" name="Costa Rica" />
  18. <neighbor direction="E" name="Colombia" />
  19. </country>
  20. </data>
  21. """
  22. # 直接从文本字符串,获取根节点
  23. root = ET.XML(content)
  24. # --------假设获取了根节点-------
  25. # -------这里使用find,根据country名称,获取country对象--------
  26. country_obj = root.find('country')
  27. """这里需要注意的是:获取的只是第一个country节点,其余的获取不了"""
  28. # country_obj的三种属性,可以打印出来
  29. print(country_obj.tag,country_obj.attrib,country_obj.text)
  30. # 输出是:country {'name':'Liechtenstein'}

2.获取第一个子节点属性后,获取子节点的子节点

  1. from xml.etree import ElementTree as ET
  2. content = """
  3. <data>
  4. <country name="Liechtenstein">
  5. <rank>2</rank>
  6. <year>2023</year>
  7. <gdppc>141100</gdppc>
  8. <neighbor direction="E" name="Austria" />
  9. <neighbor direction="W" name="Switzerland" />
  10. </country>
  11. <country name="Panama">
  12. <rank>69</rank>
  13. <year>2026</year>
  14. <gdppc>13600</gdppc>
  15. <neighbor direction="W" name="Costa Rica" />
  16. <neighbor direction="E" name="Colombia" />
  17. </country>
  18. </data>
  19. """
  20. root = ET.XML(content)
  21. v1 = root.find('country').find('rank')
  22. print(v1.text)

但是find只能获取一个节点,如果节点重名,其他节点想获取的话怎么办呢?

二.findall:获取所有这个名称的子节点

root.findall(‘节点名称’)会把root下所有这个名称的节点对象抓出来,返回类型是一个列表.
这个有什么用呢?

  1. # 需求:把下面content里根标签下第二个country节点 的属性打印出来
  2. content = """
  3. <data>
  4. <country name="Liechtenstein">
  5. <rank>2</rank>
  6. <year>2023</year>
  7. <gdppc>141100</gdppc>
  8. <neighbor direction="E" name="Austria" />
  9. <neighbor direction="W" name="Switzerland" />
  10. </country>
  11. <country name="Panama">
  12. <rank>69</rank>
  13. <year>2026</year>
  14. <gdppc>13600</gdppc>
  15. <neighbor direction="W" name="Costa Rica" />
  16. <neighbor direction="E" name="Colombia" />
  17. </country>
  18. </data>
  19. """

这个时候用findall打印就非常方便了

  1. from xml.etree import ElementTree as ET
  2. content = """
  3. <data>
  4. <country name="Liechtenstein">
  5. <rank>2</rank>
  6. <year>2023</year>
  7. <gdppc>141100</gdppc>
  8. <neighbor direction="E" name="Austria" />
  9. <neighbor direction="W" name="Switzerland" />
  10. </country>
  11. <country name="Panama">
  12. <rank>69</rank>
  13. <year>2026</year>
  14. <gdppc>13600</gdppc>
  15. <neighbor direction="W" name="Costa Rica" />
  16. <neighbor direction="E" name="Colombia" />
  17. </country>
  18. </data>
  19. """
  20. root = ET.XML(content)
  21. print(root.findall('country')[1].attrib)