xPath 用法總結整理


最近在研究kafka,看了一堆理論的東西,想動手實踐一些東西,奈何手上的數據比較少,突發奇想就打算寫個爬蟲去抓一些數據來玩,順便把深入一下爬蟲技術。

之前寫過一些小爬蟲,一般就是用python的requests+lxml來爬取數據。這次打算學一下python的scrapy框架來爬取數據。解析網頁內容還是打算用lxml,lxml使用了xpath語法,由於太久沒用都忘光了。所以打算重新學習一下xpath語法,並做個總結,方便以后忘了可以馬上回顧。

一、xpath介紹

XPath 是一門在 XML 文檔中查找信息的語言。XPath 用於在 XML 文檔中通過元素和屬性進行導航。

  • XPath 使用路徑表達式在 XML 文檔中進行導航
  • XPath 包含一個標准函數庫
  • XPath 是 XSLT 中的主要元素
  • XPath 是一個 W3C 標准

節點

在 XPath 中,有七種類型的節點:元素、屬性、文本、命名空間、處理指令、注釋以及文檔(根)節點。XML 文檔是被作為節點樹來對待的。

二、xpath語法

表達式 描述
nodename 選取此節點的所有子節點。
/ 從根節點選取。
// 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。
. 選取當前節點。
.. 選取當前節點的父節點。
@ 選取屬性。

例子

以下面這個xml為例子

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>
  • xml.xpath(“bookstore”) 表示選取 bookstore 元素的所有子節點
  • xml.xpath(“/bookstore”) 表示選取根元素 bookstore。
  • xml.xpath(“bookstore/book”) 選取屬於 bookstore 的子元素的所有 book 元素。
  • xml.xpath(“//book”) 選取所有 book 子元素,而不管它們在文檔中的位置。
  • xml.xpath(“bookstore//book”) 選擇屬於 bookstore 元素的后代的所有 book 元素,而不管它們位於 bookstore 之下的什么位置。
  • xml.xpath(“//@lang”) 選取名為 lang 的所有屬性。

謂語

路徑表達式 結果
/bookstore/book[1] 選取屬於 bookstore 子元素的第一個 book 元素。
/bookstore/book[last()] 選取屬於 bookstore 子元素的最后一個 book 元素。
/bookstore/book[last()-1] 選取屬於 bookstore 子元素的倒數第二個 book 元素。
/bookstore/book[position()<3] 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。
//title[@lang] 選取所有擁有名為 lang 的屬性的 title 元素。
//title[@lang=’eng’] 選取所有 title 元素,且這些元素擁有值為 eng 的 lang 屬性。
/bookstore/book[price>35.00] 選取 bookstore 元素的所有 book 元素,且其中的 price 元素的值須大於 35.00。
/bookstore/book[price>35.00]/title 選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大於 35.00。

選取未知節點

通配符 描述
* 匹配任何元素節點。
@* 匹配任何屬性節點。
node() 匹配任何類型的節點。

例子:

路徑表達式 結果
/bookstore/* 選取 bookstore 元素的所有子元素。
//* 選取文檔中的所有元素。
//title[@*] 選取所有帶有屬性的 title 元素。

選取若干路徑

通過在路徑表達式中使用“|”運算符,您可以選取若干個路徑。

  • //book/title | //book/price 選取 book 元素的所有 title 和 price 元素。
  • //title | //price 選取文檔中的所有 title 和 price 元素。
  • /bookstore/book/title | //price 選取屬於 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。

三、軸

軸可定義相對於當前節點的節點集。

軸名稱 結果
ancestor 選取當前節點的所有先輩(父、祖父等)。
ancestor-or-self 選取當前節點的所有先輩(父、祖父等)以及當前節點本身。
attribute 選取當前節點的所有屬性。
child 選取當前節點的所有子元素。
descendant 選取當前節點的所有后代元素(子、孫等)。
descendant-or-self 選取當前節點的所有后代元素(子、孫等)以及當前節點本身。
following 選取文檔中當前節點的結束標簽之后的所有節點。
namespace 選取當前節點的所有命名空間節點。
parent 選取當前節點的父節點。
preceding 選取文檔中當前節點的開始標簽之前的所有節點。
preceding-sibling 選取當前節點之前的所有同級節點。
self 選取當前節點。

步的語法:
軸名稱::節點測試[謂語]

例子:

例子 結果
child::book 選取所有屬於當前節點的子元素的 book 節點。
attribute::lang 選取當前節點的 lang 屬性。
child::* 選取當前節點的所有子元素。
attribute::* 選取當前節點的所有屬性。
child::text() 選取當前節點的所有文本子節點。
child::node() 選取當前節點的所有子節點。
descendant::book 選取當前節點的所有 book 后代。
ancestor::book 選擇當前節點的所有 book 先輩。
ancestor-or-self::book 選取當前節點的所有 book 先輩以及當前節點(如果此節點是 book 節點)
child::*/child::price 選取當前節點的所有 price 孫節點。

四、一些函數

1. starts-with函數

獲取以xxx開頭的元素
例子:xpath(‘//div[stars-with(@class,”test”)]’)

2 contains函數

獲取包含xxx的元素
例子:xpath(‘//div[contains(@id,”test”)]’)

3 and

與的關系
例子:xpath(‘//div[contains(@id,”test”) and contains(@id,”title”)]’)

4 text()函數

例子1:xpath(‘//div[contains(text(),”test”)]’)
例子2:xpath(‘//div[@id=”“test]/text()’)

五、一個lxml的xpath示例

下面這個代碼來自http://www.cnblogs.com/descusr/archive/2012/06/20/2557075.html

#coding=utf-8

from lxml import etree

html = ''' <html>   <head>     <meta name="content-type" content="text/html; charset=utf-8" />     <title>友情鏈接查詢 - 站長工具</title>     <!-- uRj0Ak8VLEPhjWhg3m9z4EjXJwc -->     <meta name="Keywords" content="友情鏈接查詢" />     <meta name="Description" content="友情鏈接查詢" />   </head>   <body>     <h1 class="heading">Top News</h1>     <p style="font-size: 200%">World News only on this page</p>     Ah, and here's some more text, by the way.     <p>... and this is a parsed fragment ...</p>     <a href="http://www.cydf.org.cn/" rel="nofollow" target="_blank">青少年發展基金會</a>     <a href="http://www.4399.com/flash/32979.htm" target="_blank">洛克王國</a>     <a href="http://www.4399.com/flash/35538.htm" target="_blank">奧拉星</a>     <a href="http://game.3533.com/game/" target="_blank">手機游戲</a>     <a href="http://game.3533.com/tupian/" target="_blank">手機壁紙</a>     <a href="http://www.4399.com/" target="_blank">4399小游戲</a>     <a href="http://www.91wan.com/" target="_blank">91wan游戲</a>   </body> </html> '''
page = etree.HTML(html.lower().decode('utf-8'))
hrefs = page.xpath(u"//a")
for href in hrefs:
    print href.attrib

打印出的結果為:

{‘href’: ‘http://www.cydf.org.cn/‘, ‘target’: ‘_blank’, ‘rel’: ‘nofollow’}
{‘href’: ‘http://www.4399.com/flash/32979.htm‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.4399.com/flash/35538.htm‘, ‘target’: ‘_blank’}
{‘href’: ‘http://game.3533.com/game/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://game.3533.com/tupian/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.4399.com/‘, ‘target’: ‘_blank’}
{‘href’: ‘http://www.91wan.com/‘, ‘target’: ‘_blank’}

如果要獲取標簽a之間的內容,就可以用print href.text輸出

六、總結

上面的內容大多都是抄自網上的一些資料。這里只是做了一個大概的總結,后面如果有漏的還會補充。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM