pythonxmlxpath,Python使用XPath解析XML文档
pythonxmlxpath,Python使⽤XPath解析XML⽂档XPath(XML Path Language)是⼀种⼩型的查询语⾔:
1)可在XML中查信息
2)⽀持HTML的查
3)通过元素和属性进⾏导航
python开发使⽤XPath条件:
由于XPath属于lxml库模块,所以⾸先要安装库lxml
XPath的使⽤⽅法:
⾸先讲⼀下XPath的基本语法知识:
四种标签的使⽤⽅法
张姓女孩名字大全1) // 双斜杠 定位根节点,会对全⽂进⾏扫描,在⽂档中选取所有符合条件的内容,以列表的形式返回。
2) / 单斜杠 寻当前标签路径的下⼀层路径标签或者对当前路标签内容进⾏操作。
3) /text() 获取当前路径下的⽂本内容。
4) /@xxxx 提取当前路径下标签的属性值
x战警系列顺序
5) | 可选符 使⽤|可选取若⼲个路径 如//p | //div 即在当前路径下选取所有符合条件的p标签和div标签
6) . 点 ⽤来选取当前节点
7) .. 双点 选取当前节点的⽗节点
注:以下代码在Python3.6.1下调试通过进京需要核酸检测吗
⼀、Python读取XML简单实例
from lxml import etree
text = """
1
2008
141100
4
2011
59900
68
2011
13600
"""
root = etree.XML(text)
夜情人print('rank',child.xpath('./rank/text()')[0])
print('year', child.xpath('./year/text()')[0])
print('gdppc', child.xpath('./gdppc/text()')[0])
print('neighbor', child.xpath('./neighbor/@name')[0])
from lxml import etree
text = """
1
2008
车辆工程专业就业141100
4
2011
59900
68
2011
13600
"""root = etree.XML(text)
for child in root:
print(child.tag, child.attrib)
print('rank',child.xpath('./rank/text()')[0])
print('year', child.xpath('./year/text()')[0])
print('gdppc', child.xpath('./gdppc/text()')[0])
print('neighbor', child.xpath('./neighbor/@name')[0])
⼆、Python读取本地XML简单实例
如果XML⽂档存放在本地,可以⽂件对象打开xml就读取成字符串类型,也可以直接tree = etree.parse(“D:/l”)读取。etree.parse("D:/l")读取。
from lxml import etree
tree = etree.parse("D:/l")
root = t()
for child in root:
print(child.tag, child.attrib)
print('rank',child.xpath('./rank/text()')[0])
print('year', child.xpath('./year/text()')[0])
etree.parse("D:/l")读取。
from lxml import etree
tree = etree.parse("D:/l")
root = t()for child in root:
print(child.tag, child.attrib)
print('rank',child.xpath('./rank/text()')[0])
print('year', child.xpath('./year/text()')[0])
print('gdppc', child.xpath('./gdppc/text()')[0])
print('neighbor', child.xpath('./neighbor/@name')[0])三、为防⽌读取XML出错导致程序退出,可使⽤try捕获异样from lxml import etree
tree = etree.parse("D:/l")
root = t()
root = etree.XML(text)
for child in root:
秦汉 林青霞try:
print(child.tag, child.attrib)
print('rank',child.xpath('./rank/text()')[0])
print('year', child.xpath('./year/text()')[0])
print('gdppc', child.xpath('./gdppc/text()')[0])
print('neighbor', child.xpath('./neighbor/@name')[0]) print('neighbor', child.xpath('./neighbor/@name')[1]) except Exception as e:
print(e)
from lxml import etree
tree = etree.parse("D:/l")
root = t()root = etree.XML(text)
for child in root:
try:
print(child.tag, child.attrib)
print('rank',child.xpath('./rank/text()')[0])
print('year', child.xpath('./year/text()')[0])
print('neighbor', child.xpath('./neighbor/@name')[1]) except Exception as e:
print(e)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。