python xml.etree.ElementTree解析xml文件获取节点
2023-09-27 14:28:20 时间
<?xml version = "1.0" encoding = "utf-8"?> <root> <body name="lyc"> <age>110</age> </body> <body name = "l" age = "10"> </body> </root> ###################### #coding=UTF8 from xml.etree import ElementTree #xmlText = open("xml.txt").read() #root = ElementTree.fromstring(xmlText) root = ElementTree.parse("xml.txt") bodys = root.getiterator("body") #getiterator方法获取 print "getiterator" print bodys print dir(bodys[0]) print "attrib:",bodys[0].attrib print "tag:",bodys[0].tag print "text",bodys[0].text #getchildren方法获取 print "getchildren" children = bodys[0].getchildren() print children print "attrib:",children[0].attrib print "tag:",children[0].tag print "text:",children[0].text #find print "find" children = root.find("body") print children print "attrib:",children.attrib print "tag:",children.tag print "text:",children.text #findall print "findall" children = root.findall("body") print children print "attrib:",children[0].attrib print "tag:",children[0].tag print "text:",children[0].text
aa = xml.dom.minidom.parseString(response_res).documentElement.getElementsByTagName('string')[0].childNodes[0].data
doc = etree.XML(aa)
doc.xpath("//BoardData")[0].getchildren()[0].text
相关文章
- python 2022省赛 裁剪刀
- 【Python从入门到精通】(十八)Python的文件夹操作,创建文件夹复制文件等等
- python将不同类型文件分开
- 更深层次理解Python的 列表、元组、字典、集合(工作面试学习必需掌握的知识点)
- Python常用函数:csv【CSV文件的读取、写入】
- linux python 安装 pymssql
- 面试官对我说:怎么用Python加密文件?怎么回才显得情商高?
- Python3入门(四)——Python函数
- python 爬虫系列02-小说
- python enumerate for 使用 主线程和子线程的创建和执行学习笔记
- python pip 快速安装第三方库和下载好whl文件
- 使用Python和OpenCV对轮廓进行排序(从左到右,自上而下)
- python批量创建文件与批量创建文件夹
- Advanced Python系列之协程Coroutines架构内核与源码实战
- 第4.3节 Python中与迭代相关的函数
- Python、aes.js和node.js实现AES(Crypto)加密与解密实现代码
- Python Pandas pandas.DataFrame.join函数方法的使用
- Python 如何相对于工程目录指定文件
- Python 读取 操作 使用 db文件
- 关于python文件读写小结
- python-文件读写操作
- python 写入Excel文件
- 【Python 入门】文件IO
- 十九、python学习之前端:html及css入门