Python3中xml.etree.ElementTree支持XPath语法的示例分析

发布时间:2021-06-08 11:48:37 作者:小新
来源:亿速云 阅读:216

小编给大家分享一下Python3中xml.etree.ElementTree支持XPath语法的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

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()')

看完了这篇文章,相信你对“Python3中xml.etree.ElementTree支持XPath语法的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Xpath语法
  2. xml语法的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python3 xpath

上一篇:python中GUI库图形界面开发之PyQt5信号与槽怎么用

下一篇:Python中定时器线程池原理的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》