溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-06-08 11:48:37 來源:億速云 閱讀:229 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下Python3中xml.etree.ElementTree支持XPath語法的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

xml.etree.ElementTree可以通過支持的有限的XPath表達(dá)式來定位元素。

語法

ElementTree支持的語法如下:

語法說明
tag查找所有具有指定名稱tag的子元素。例如:country表示所有名為country的元素,country/rank表示所有名為country的元素下名為rank的元素。
*查找所有元素。如:*/rank表示所有名為rank的孫子元素。
.選擇當(dāng)前元素。在xpath表達(dá)式開頭使用,表示相對路徑。
//選擇當(dāng)前元素下所有級別的所有子元素。xpath不能以“//”開頭。
..選擇父元素。如果視圖達(dá)到起始元素的祖先,則返回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ù)、表達(dá)式last()或相對于最后一個位置的位置(如:last()-1)

方括號表達(dá)式前面必須有標(biāo)簽名、星號或者其他方括號表達(dá)式。position前必須有一個標(biāo)簽名。

簡單示例

#!/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)
#查找當(dāng)前元素的父元素,結(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語法的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI