xml.etree ElementTree简介
xml.etree ElementTree介绍
ET简介
ET有重要的两个类,一个是ElementTree,另一个是Element.
ET使用
假设有xml文件内容如下:
<?xml version="1.0"?>
<data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country>
</data>
解析xml:
导入数据或者文件:parse(source, parser=None)
获取xml根:tree.getroot()
解析字符串并获取根:ET.fromstring(country_data_as_string)
parse(source, parser=None)
#source:xml文件
#parser:选择解释器实例,默认XMLParser#读入文件
tree = ET.parse('country_data.xml')
root = tree.getroot() #获取根#解析字符串获取根
root = ET.fromstring(country_data_as_string)
root的属性
root有一个标记和一个属性字典。
>>> root.tag
'data' #这个是标签名
>>> root.attrib
{} #这个是标签属性
遍历节点孩子的标签名和属性
>>> for child in root:
... print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
**通过下标锁定元素
root是根节点,root[0]是根节点下的第一个元素,root[0] [1]是根节点的第一个节点的第二个元素
>>> root[0][1].text
'2008'
获取子元素迭代器
通过Element.iter()可以获取到某元素的子元素的迭代器,如:root.iter(‘neighbor’)获取根元素下的名为neighbor的元素的迭代器
>>> for neighbor in root.iter('neighbor'):
... print(neighbor.attrib)
...
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}
查找节点(元素)
Element.findall()只查找带有标记的节点,这些元素是当前节点的直接子节点。
Element.text访问节点的文本内容
Element.find()查找第一个指定便签的子节点
Element.get()获取节点属性
>>> for country in root.findall('country'):
... rank = country.find('rank').text
... name = country.get('name')
... print(name, rank)
...
Liechtenstein 1
Singapore 4
Panama 68
更改节点信息
ElementTree.write()写入并创建xml文件
Element.set()给节点设置属性
Element.append()给节点添加新的子节点
>>> for rank in root.iter('rank'):
... new_rank = int(rank.text) + 1
... rank.text = str(new_rank)
... rank.set('updated', 'yes')
...
>>> tree.write('output.xml')
移除节点
Element.remove()能移除整个节点,包括它的子节点。
>>> for country in root.findall('country'):
... rank = int(country.find('rank').text)
... if rank > 50:
... root.remove(country)
...
>>> tree.write('output.xml')
XML文件将变成:(这里删除了一个节点,包括它的子节点)
<?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>
</data>
构建XML文件
ET.Element()创建一个节点
ET.SubElement()创建子节点
>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> c = ET.SubElement(a, 'c')
>>> d = ET.SubElement(c, 'd')
>>> ET.dump(a)
<a><b /><c><d /></c></a>
xpath表达式
ET支持xpath表达式,xpath是用来分析XML/HTML文件或数据的。我们可以通过xpath表达式来快速得锁定HTML/XML的节点或元素。在HTML中标签通常称为节点。(有一点值得注意的是,在ET中有部分xpath表达式不能直接使用)
这里就简单介绍一下xpath语法。
xpath语法
简单的语法
/ #根节点或节点间的过渡
// #跨越节点获取节点
. #选取当前节点
.. #当前节点的父节点(在ET中不能通过子节点直接获取父节点)
@ #选取属性
text() #选取文本(在ET中不能使用)
[index] #根据index获取第index个标签(index从1开始)
[@Classname] #根据属性名获取标签
contains(p,content) #模糊查询(p定位,content匹配字段)
xpath有点像文件路径的写法,很容易学习,下面的简单例子能帮助理解。
简单的例子
#根节点下的html的标签中的body标签中的div标签中的div标签
/html/body/div/div #获取所以a标签
//a#获取html的标签中的body标签中的所有a标签(可以跨越节点,不必一定是子节点)
/html/body//a#在当前节点下的a标签
./a#当前节点的父标签中的a标签
../a#根节点下的html的标签中的body标签中的a标签中的class属性
/html/body/a/@class#li便签下的第3个li标签
//li[3]#通过属性定位
//a[@href=""]#html的标签中的body标签中的a标签中的文本内容
/html/body/a/text()#获取a标签,并且属性name中有字段"myname"
//a[contains(@name,"myname")]
其他xpath博客连接: https://blog.csdn.net/qq_43203949/article/details/108203340.
在ET上使用xpath
如果学过parsel模块包的,把findall()当成extract()或者getall()方法就可以了。
import xml.etree.ElementTree as ETroot = ET.fromstring(countrydata)# Top-level elements(当前节点就是根节点)
root.findall(".")# All 'neighbor' grand-children of 'country' children of the top-level
# elements
root.findall("./country/neighbor")# Nodes with name='Singapore' that have a 'year' child
root.findall(".//year/..[@name='Singapore']")# 'year' nodes that are children of nodes with name='Singapore'
root.findall(".//*[@name='Singapore']/year")# All 'neighbor' nodes that are the second child of their parent
root.findall(".//neighbor[2]")
xml.etree ElementTree简介相关推荐
- Python 标准库之 xml.etree.ElementTree xml解析
Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...
- xml 属性value换行显示_python 标准库之xml.etree.ElementTree
简介 Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属 ...
- python【模块】xml.etree.ElementTree 解析 xml
pytho 模块 xml.etree.ElementTree 解析 xml 文章目录 pytho 模块 xml.etree.ElementTree 解析 xml 1. 什么是 XML? 2. Elem ...
- python etree库_python 标准库之xml.etree.ElementTree
简介 Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. [注意]xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. 每个element对象都具有以下属 ...
- [PYTHON] xml.etree.ElementTree 实例
2019独角兽企业重金招聘Python工程师标准>>> --------------------------------------------------------------- ...
- python里遍历筛选xml文件_python xml.etree.ElementTree遍历xml所有节点实例详解
python xml.etree.ElementTree遍历xml所有节点 XML文件内容: 代码: #-*- coding: UTF-8 -*- # 从文件中读取数据 import xml.etre ...
- python-23 xml.etree.ElementTree模块
xml.etree.ElementTree模块 Element类型是一种灵活的容器对象,用于在内存中存储结构化数据. xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全. ...
- python xml etree_python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用...
1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...
- python 打印xml文档树_[Python]xml.etree.ElementTree处理xml文档
需求: 在实际应用中,需要对xml配置文件进行实时修改, 1.增加.删除 某些节点 2.增加,删除,修改某个节点下的某些属性 3.增加,删除,修改某些节点的文本 xml源文件格式[例] path=&q ...
最新文章
- cad打开图纸流程图_如何一键打开超大CAD图纸,进行CAD快速看图?
- 用百度开放地图api在代码中获得两地距离
- Java中this()和super()的注意点
- Java学习笔记十五
- ASP.NET Core 中间件分类
- Ubuntu下GTK的安装、编译和测试
- zookeeper入门综合概要介绍
- java基础回顾(一)—— sleep和wait的区别
- Setup Factory 安装程序的图标
- 单个dcm文件含有多帧数据,如何拆分成多个dcm文件
- C#—软件注册与注册机
- android短信验证码自动填写
- wineqq解决字体问题
- 疫情大考,文旅央企华侨城出台有力措施保障游客安全
- Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
- MacBook Pro 2018电池鼓包、键盘问题免费换新
- 二手书交易系统数据流图
- 爬虫实战入门级教学(数据爬取->数据分析->数据存储)
- 漫说测试 | 研发虐我千百遍,我待bug如初恋
- nginx lingering_close
热门文章
- 连接池 druid(阿里巴巴的框架)
- jetbrick-template 1.1.0 发布,支持 #tag, #macro, layout
- DNS高级部署使用RSYNC部署搭建DNS view主从服务
- 请指点一下,讨论也可以,顶也有分
- 长短时记忆网络(LSTM)部分组件(六)
- kafka集群的搭建
- 利用PostMan 模拟上传/下载文件(亲测)
- PHP源码分析-PHP的生命周期
- 快速排序,排序一亿数据用时14秒
- ElementUI的el-cascader级联选择器各个属性的设置