xml.etree.ElementTree可以通过支持的有限的XPath表达式来定位元素。

语法

ElementTree支持的语法如下:

语法 说明
tag 查找所有具有指定名称tag的子元素。例如:country表示所有名为country的元素,country/rank表示所有名为country的元素下名为rank的元素。
* 查找所有元素。如:*/rank表示所有名为rank的孙子元素。
. 选择当前元素。在xpath表达式开头使用,表示相对路径。
// 选择当前元素下所有级别的所有子元素。xpath不能以“//”开头。
.. 选择父元素。如果视图达到起始元素的祖先,则返回None(或空列表)。起始元素为调用find(或findall)的元素。
[@attrib] 选择具有指定属性attrib的所有子元素。
[@attrib='value'] 选择指定属性attrib具有指定值value的元素,该值不能包含引号。
[tag] 选择所有具有名为tag的子元素的元素。
[.='text'] Python3.7+,选择元素(或其子元素)完整文本内容为指定的值text的元素。
[tag='text'] 选择元素(或其子元素)名为tag,完整文本内容为指定的值text的元素。
[position] 选择位于给定位置的所有元素,position可以是以1为起始的整数、表达式last()或相对于最后一个位置的位置(如:last()-1)

方括号表达式前面必须有标签名、星号或者其他方括号表达式。position前必须有一个标签名。

简单示例

#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
import xml.etree.cElementTree as ET
xml_string="""<?xml version="1.0"?>
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2008</year>
    <gdppc>141100</gdppc>
    <neighbor name="Austria" direction="E"/>
    <neighbor name="Switzerland" direction="W"/>
  </country>
  <country name="Singapore">
    <rank updated="yes">5</rank>
    <year>2011</year>
    <gdppc>59900</gdppc>
    <neighbor name="Malaysia" direction="N"/>
  </country>
  <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2011</year>
    <gdppc>2011</gdppc>
    <neighbor name="Costa Rica" direction="W"/>
    <neighbor name="Colombia" direction="E"/>
  </country>
	<country name="Washington">
    <rank updated="yes">55</rank>
    <gdppc>13600</gdppc>
  </country>
</data>
"""
root=ET.fromstring(xml_string)
#查找data下所有名为country的元素
for country in root.findall("country"):
	print("name:"+country.get("name"))
	#查找country下所有名为year的元素
	year=country.find("./year")
	if year:
		print("year:"+year.text)
#查找名为neighbor的孙子元素
for neighbor in root.findall("*/neighbor"):
	print("neighbor:"+neighbor.get("name"))
#查找country下的所有子元素
for ele in root.findall("country//"):
	print(ele.tag)
#查找当前元素的父元素,结果为空
print(root.findall(".."))
#查找与名为rank的孙子元素同级的名为gdppc的元素
for gdppc in root.findall("*/rank/../gdppc"):
	print("gdppc:"+gdppc.text)
#查找data下所有具有name属性的子元素
for country in root.findall("*[@name]"):
	print(country.get("name"))
#查找neighbor下所有具有name属性的子元素
for neighbor in root.findall("country/*[@name]"):
	print(neighbor.get("name"))
#查找country下name属性值为Malaysia的子元素
print("direction:"+root.find("country/*[@name='Malaysia']").get("direction"))
#查找root下所有包含名为year的子元素的元素
for country in root.findall("*[year]"):
	print("name:"+country.get("name"))
#查找元素(或其子元素)文本内容为2011的元素(Python3.7+)
#print(len(root.findall("*[.='2011']")))
#查找元素(或其子元素)名为gdppc,文本内容为2011的元素
for ele in root.findall("*[gdppc='2011']"):
	print(ele.get("name"))
#查找第二个country元素
print(root.find("country[2]").get("name"))

补充知识:python lxml etree xpath定位

etree全称:ElementTree 元素树

用法:

import requests
from lxml import etree
response = requests.get('html')
res = etree.HTML(response.text)   #利用 etree.HTML 初始化网页内容
resp = res.xpath('//span[@class="green"]/text()')