最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3 xml.etree.ElementTree支持的XPath語法詳解

 更新時間:2020年03月06日 12:50:12   作者:_榮耀之路_  
這篇文章主要介紹了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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

荥阳市| 枣强县| 庄河市| 雅江县| 康保县| 海门市| 瓦房店市| 保康县| 公安县| 旌德县| 东平县| 溧阳市| 乌海市| 马关县| 遂平县| 榆社县| 天祝| 揭阳市| 茂名市| 罗山县| 海林市| 昌宁县| 太康县| 台东市| 乌拉特中旗| 安溪县| 沙雅县| 南安市| 视频| 涟源市| 肃南| 周宁县| 高清| 金坛市| 施秉县| 淮滨县| 昂仁县| 琼海市| 平泉县| 安吉县| 赤水市|