xpath用法講解
文章轉自:xPath 用法總結整理
一、xpath介紹
XPath 是一門在 XML 文檔中查找信息的語言。XPath 用於在 XML 文檔中通過元素和屬性進行導航。
- XPath 使用路徑表達式在 XML 文檔中進行導航
- XPath 包含一個標準函數庫
- XPath 是 XSLT 中的主要元素
- XPath 是一個 W3C 標準
節點
在 XPath 中,有七種類型的節點:元素、屬性、文本、命名空間、處理指令、註釋以及文檔(根)節點。XML 文檔是被作爲節點樹來對待的。
二、xpath語法
表達式 | 描述 |
---|
例子
以下面這個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 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 的所有屬性。
謂語
路徑表達式 | 結果 |
---|
選取未知節點
通配符 | 描述 |
---|
例子:
路徑表達式 | 結果 |
---|
選取若干路徑
通過在路徑表達式中使用“|”運算符,您可以選取若干個路徑。
//book/title | //book/price
選取 book 元素的所有 title 和 price 元素。//title | //price
選取文檔中的所有 title 和 price 元素。/bookstore/book/title | //price
選取屬於 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 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
-
#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
輸出
六、總結
上面的內容大多都是抄自網上的一些資料。這裏只是做了一個大概的總結,後面如果有漏的還會補充。