Python3 xml.etree.ElementTree支持的XPath語法詳解
xml.etree.ElementTree可以通過支持的有限的XPath表達式來定位元素。
語法
ElementTree支持的語法如下:
| 語法 | 說明 |
| tag | 查找所有具有指定名稱tag的子元素。例如:country表示所有名為country的元素,country/rank表示所有名為country的元素下名為rank的元素。 |
| * | 查找所有元素。如:*/rank表示所有名為rank的孫子元素。 |
| . | 選擇當前元素。在xpath表達式開頭使用,表示相對路徑。 |
| // | 選擇當前元素下所有級別的所有子元素。xpath不能以“//”開頭。 |
| .. | 選擇父元素。如果視圖達到起始元素的祖先,則返回None(或空列表)。起始元素為調(diào)用find(或findall)的元素。 |
| [@attrib] | 選擇具有指定屬性attrib的所有子元素。 |
| [@attrib='value'] | 選擇指定屬性attrib具有指定值value的元素,該值不能包含引號。 |
| [tag] | 選擇所有具有名為tag的子元素的元素。 |
| [.='text'] | Python3.7+,選擇元素(或其子元素)完整文本內(nèi)容為指定的值text的元素。 |
| [tag='text'] | 選擇元素(或其子元素)名為tag,完整文本內(nèi)容為指定的值text的元素。 |
| [position] | 選擇位于給定位置的所有元素,position可以是以1為起始的整數(shù)、表達式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)
#查找當前元素的父元素,結(jié)果為空
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"))
#查找元素(或其子元素)文本內(nèi)容為2011的元素(Python3.7+)
#print(len(root.findall("*[.='2011']")))
#查找元素(或其子元素)名為gdppc,文本內(nèi)容為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 初始化網(wǎng)頁內(nèi)容
resp = res.xpath('//span[@class="green"]/text()')
以上這篇Python3 xml.etree.ElementTree支持的XPath語法詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作
這篇文章主要介紹了pytorch快速搭建神經(jīng)網(wǎng)絡(luò)_Sequential操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
基于windows下pip安裝python模塊時報錯總結(jié)
今天小編就為大家分享一篇基于windows下pip安裝python模塊時報錯總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
PyTorch解決ModuleNotFoundError: No module named
本文主要介紹了PyTorch解決ModuleNotFoundError: No module named ‘torch’,這個錯誤意味著我們的Python環(huán)境中沒有安裝PyTorch庫,無法正常使用其功能,下面就來具體介紹一下2024-03-03
python框架flask入門之路由及簡單實現(xiàn)方法
這篇文章主要介紹了python框架flask入門路由及路由簡單實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

