Python爬蟲經常使用:XPath語言

 XPath (XML Path Language),即 XML 路徑語言,它是一門在 XML文檔中查找信息的語言,也適用於HTML 文檔的搜索。html

------------ 簡注 --------------------------------------------ide

  XML 指可擴展標記語言,被設計用來傳輸和存儲數據。函數

  HTML 指超文本標記語言,被設計用來顯示數據(描述網頁)。spa

---------------------------------------------------------------設計


  XPath 使用路徑表達式來選取 XML 文檔中的節點或者節點集。3d

------------ 簡注 --------------------------------------------htm

在 XPath 中,有七種類型的節點:元素、屬性、文本、blog

命名空間、處理指令、註釋以及文檔(根)節點。教程

XML 文檔是被做爲節點樹來對待的。圖片

樹的根被稱爲文檔節點或者根節點。

---------------------------------------------------------------


  XPath 包含一個標準函數庫,這些函數用於字符串、數值、時間的匹配以及節點、序列的處理等。

1、XPath 經常使用表達式

圖片

2、XPath 經常使用函數

position() 返回當前正在被處理節點的 index 位置。

last() 返回當前被處理節點列表中的項目數目。

圖片


用法示例

示例文檔

圖片


1) 選取全部節點

a. 從當前節點 (根節點) 選取全部子孫節點與元素名

demo1 = html.xpath('//*')
print(demo1)
# 運行結果:
'''[<Element html at 0x1a3caa23688>, 

<Element body at 0x1a3caa23808>,
<Element bookstore at 0x1a3caa237c8>, 

<Element book at 0x1a3caa23848>,
<Element title at 0x1a3caa23888>, 

<Element price at 0x1a3caa23908>,
<Element book at 0x1a3caa23948>, 

<Element title at 0x1a3caa23988>,
<Element price at 0x1a3caa23c08>]'''

b. 選取全部元素名爲 book 的子節點,無需考慮位置

demo2 = html.xpath('//book')
print(demo2)
# 運行結果:
'''[<Element book at 0x204fc5a3808>, 

<Element book at 0x204fc5a37c8>]'''


2) 選取子節點

a. 選取 book 節點下的全部子節點

demo3 = html.xpath('//book/*')
print(demo3)
# 運行結果:
'''[<Element title at 0x1c6e5eb3848>,
<Element price at 0x1c6e5eb3808>,
<Element title at 0x1c6e5eb3888>,
<Element price at 0x1c6e5eb38c8>]'''

b. 選取 book 節點的 title 子節點

demo4 = html.xpath('//book/title')
print(demo4)
# 運行結果:
'''[<Element title at 0x200f3443848>, 

<Element title at 0x200f3443808>]'''


3) 選取父節點

選取 title 節點的父節點

demo5 = html.xpath('//title/..')
print(demo5)
# 運行結果:
'''[<Element book at 0x26b22553848>, 

<Element book at 0x26b22553808>]'''


4) 屬性獲取 / 屬性匹配

a. 選取 lang 屬性

demo6 = html.xpath('//@lang')
print(demo6)
# 運行結果:
'''['eng', 'eng']'''

b. 選取屬性爲 lang 的全部 title 元素

demo7 = html.xpath('//title[@lang]')
print(demo7)
# 運行結果:
'''[<Element title at 0x283dee53848>,

 <Element title at 0x283dee53808>]'''

c. 選取 lang 屬性的值爲 eng 全部 title 元素

demo8 = html.xpath('//title[@lang="eng"]')
print(demo8)
# 運行結果:
'''[<Element title at 0x251380c3808>, 

<Element title at 0x251380c37c8>]'''


5) 文本獲取

demo9 = html.xpath('//title/text()')
print(demo9)
# 運行結果:
'''['Harry Potter', 'Learning XML']'''


6) 按序獲取

a. 選取第一個 book 元素的 title

demo10 = html.xpath('//bookstore/book[1]/title/text()')
print(demo10)
# 運行結果:
'''['Harry Potter']'''

b. 選取最後一個 book 元素的 title 

demo11 = html.xpath(

                 '//bookstore/book[last()]/title/text()')
print(demo11)
# 運行結果:
'''['Learning XML']'''

c. 選取倒數第二個 book 元素的 price

demo12 = html.xpath(

          '//bookstore/book[last()-1]/price/text()')
print(demo12)
# 運行結果:
'''['29.99']'''

d. 選取前兩個 book 元素的 price

demo13 = html.xpath(

 '//bookstore/book[position()<3]/price/text()')
print(demo13)
# 運行結果:
'''['29.99', '39.95']'''


參考文檔

XPath 教程:

https://www.w3school.com.cn/xpath/index.asp


圖片

https://mp.weixin.qq.com/s/SENEa6jBCyDVt5fMiOiXrw

相關文章
相關標籤/搜索