Python xpath表示式如何實現資料處理

2022-10-04 14:15:50 字數 3108 閱讀 9336

xpath表示式

1. xpath語法

vrdsiwoqrookstore>

harry potter

999learning xml

888

1.1 選取節點

xpath 使用路徑表示式來選取 xml 文件中的節點或者節點集。這些路徑表示式和我們在常規的電腦檔案系統中看到的表示式非常相似。

使用chrome外掛程式選擇標籤時候,選中時,選中的標籤會新增屬性class="xh-highlight"

下面列出了最有用的表示式:

表示式描述

nodename

選中該元素。

/從根節點擊取、或者是元素和元素間的過渡。

//從匹配選擇的當前節點擊擇文件中的節點,而不考慮它們的位置。

.選取當前節點。

..選取當前節點的父節點。

@選取屬性。

text()

選取文字。

例項路徑表示式

結果bookstore

選擇bookstore元素。

/bookstore

選取根元素 bookstore。注釋:假如路徑起始於正斜槓( / ),則此路徑始終代表到某元素的絕對路徑!

bookstore/book

選取屬於 bookstore 的子元素的所有 book 元素。

//book

選取所有 book 子元素,而不管它們在文件中的位置。

bookstore//book

選擇屬於 bookstore 元素的後代的所有 book 元素,而不管它們位於 bookstore 之下的什麼位置。

//book/title/@lang

選擇所有的book下面的title中的lang屬性的值。

//book/titl程式設計客棧e/text()

選擇所有的book下面的title的文字。

1.2 查詢特定的節點

路徑表示式

結果//title[@lang="eng"]

選擇lang屬性值為eng的所有title元素

/bookstore/book[1]

選取屬於 bookstore 子元素的第乙個 book 元素。

/bookstore/book[last()]

選取屬於 子元素的最後乙個 book 元素。

/bookstore/book[last()-1]

選取屬於 bookstore 子元素的倒數第二個 book 元素。

/bookstore/book[position()>1]

選擇bookstore下面的book元素,從第二個開始選擇

//book/title[text()='harry potter']

選擇所有book下的title元素,僅僅選擇文字為harry potter的title元素

/bookstore/book[price>35.00]/title

選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大於 35.00。

注意點: 在xpath中,第乙個元素的位置是1,最後乙個元素的位置是last(),倒數第二個是last()-1

1.3 選取未知節點

xpath 萬用字元可用來選取未知的 xml 元素。

萬用字元描述

*匹配任何元素節點。

@*匹配任何屬性節點。

node()

匹配任何型別的節點。

例項在下面的**中,我們列出了一些路徑表示式,以及這些表示式的結果:

路徑表示式

結果/bookstore/*

選取 bookstore 元素的所有子元素。

//*選取文件中的所有元素。

//title[@*]

選取所有帶有屬性的 title 元素。

1.4 選取若干路徑

通過在路徑表示式中使用「|」運算子,您可以選取若干個路徑。

例項在下面的**中,我們列出了一些路徑表示式,以及這些表示式的結果:

路徑表示式

結果//book/title | //book/price

選取 book 元素的所有 title 和 price 元素。

//title | //price

選取文件中的所有 tvrdsiwoqritle 和 price 元素。

/bookstore/book/title | //price

選取屬於 bookstore 元素的 book 元素的所有 title 元素,以及文件中所有的 price 元素。

例項:from lxml import etree

text = '''

'''html = etree.html(text)

#獲取href的列表和title的列表

href_list = html.xpath("//li[@class='item-1']/a/@href")

title_list = html.xpath("//li[@class='item-1']/a/text()")

#組裝成字典

for href in href_list:

item = {}

item["href"] = href

item["title"] = title_list[href_list.index(href)]

print(item)

# 如果取到的是乙個節點,返回的是element物件,可以繼續使用xpath方法,對此我們可以在後面的資料提取過程中:先根據某個標籤進行分組,分組之後再進行資料的提取

li_list = html.xpath("//li[@class='item-1']")

#在每一組中繼續進行資料的提取

for li in li_list:

item = {}

item["href"] = li.xpath("./a/@href")[0] if len(li.xpath("./a/@href"))>0 else none

item["title"] = li.xpath("./a/text()")[0] if len(li.xpath("./a/text()"))>0 else none

print(item)

本文標題: python xpath表示式如何實現資料處理

本文位址:

Shell表示式,如 file

2017年10月26日 15 24 40 閱讀數 343 複製 如下 file dir1 dir2 dir3 my.file.txt 可以用 分別替換得到不同的值 複製 如下 刪掉第乙個 及其左邊的字串 dir1 dir2 dir3 my.file.txt 刪掉最後乙個 及其左邊的字串 my.fil...

Shell表示式,如 file

假設我們定義了乙個變數file file dir1 dir2 dir3 my.file.txt 可以用 分別替換得到不同的值 刪掉第乙個 及其左邊的字串 dir1 dir2 dir3 my.file.txt 刪掉最後乙個 及其左邊的字串 my.file.txt 刪掉第乙個 及其左邊的字串 file....

表示式 表示式樹 表示式求值

總時間限制 1000ms 記憶體限制 65535kb 描述 眾所周知,任何乙個表示式,都可以用一棵表示式樹來表示。例如,表示式a b c,可以表示為如下的表示式樹 a b c 現在,給你乙個中綴表示式,這個中綴表示式用變數來表示 不含數字 請你將這個中綴表示式用表示式二叉樹的形式輸出出來。輸入輸入分...