XPath簡介、功能及使用方法

 

html = '''<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title a " name="dromouse"><b>The Dormouse's story</b></p>
<p class="story b">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
</body></html>'''html

XPath簡介node

XPath,全稱XML Path Language,即XML路徑語言,能夠在XML,HTML文檔中查找信息的語言,XPath的選擇功能十分強大,提供了很是簡明瞭的路徑選擇表達式。更多的文檔能夠訪問其官方網站,http://www.w3.org/TR/xpath/。網站

XPath經常使用規則spa

nodename   選擇此節點的全部子節點code

/  從當前節點選擇直接子節點orm

// 從當前節點選擇子孫節點xml

. 選取當前節點htm

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

 @  選取屬性three

準備工做

安裝lxml   命令管理窗口 pip install lxml

XPath功能

from lxml import etree
html_data = etree.HTML(html) #聲明爲HTML文本 且etree能夠自動修復HTML文本
print(etree.tostring(html_data).decode("utf-8")) #tostring輸出完善後的html文本 decode將 bytes->str類型

全部節點  

result = html_data.xpath("//*") #表示選取全部節點
result1 = html_data.xpath("//p") # 表示選取全部p節點
print(result1)
#運行結果爲
[<Element p at 0x1f7d6abf888>, <Element p at 0x1f7d67e4748>, <Element p at 0x1f7d67e4288>]

 子節點 

result_children = html_data.xpath("//body/p")  #選取body節點裏全部p節點
result_children1 = html_data.xapth("//body/a")
print(result_children1)
#運行結果爲
[]
#經過上述程序代表//用於獲取子孫節點 /用於獲取直接子節點 ,而a節點不爲body節點的直接子節點

父節點

#第一種方法
print
(html_data.xpath('//a[@class="sister"]/../@class')) #獲取a節點父節點p節點的class對應的值a節點爲第一個a節點
#運行結果爲
['story']
第二種方法
result = html_data.xpath('//a[@class="sister"]/parent::*/@class') #經過parent::來獲取父節點

屬性匹配

result = html_data.xpath('//a[@class="sister"]') #選取全部class值爲sister的節點
print(result)
運行結果爲
[<Element a at 0x1f7d696d848>, <Element a at 0x1f7d67e4288>, <Element a at 0x1f7d6abf888>]
#獲取文本
result = html_data.xpath('//a[2][@class="sister"]/./text()') #得到第二個a節點的文本值
print(result)
#運行結果爲
['Lacie']
#獲取屬性值
result = html_data.xpath('//a[2][@class="sister"]/@href') #獲取第二個a節點的href值
print(result)
#運行結果爲
['http://example.com/lacie']

多種屬性值匹配

 

result = html_data.xpath('//p[contains(@class,"b")]/a/text()') #獲取全部p節點下的a節點的文本
print(result)
#運行結果爲
['Lacie', 'Tillie']
#多屬性匹配
 result = html_data.xpath('//a[contains(@class,"sister") and @id="link2"]//text()') #獲取第二個節點a的文本值
print(result)
#輸出結果爲
['Lacie']

 

按序選擇

result = html_data.xpath('//a[last()-1]//text()') #選取倒數第二個a節點的文本值
result1=html_data.xpath('//a[position()<3]//text()') #選取位置序號小於3的a節點 因爲第一個a節點的文本值爲空
#輸出結果爲
print(result)
['Lacie']

節點軸選擇

result = html_data.xpath('//a[1][@id="link1"]/ancestor::*') #獲取全部第一個a節點的全部祖先節點
#輸出結果爲[<Element html at 0x1f7d6a35488>, <Element body at 0x1f7d67e4cc8>, <Element p at 0x1f7d67e4ec8>]
result = html_data.xpath('//a[1][@id="link1"]/ancestor::p')#獲取p祖先節點
#接下來便再也不提供輸出結果,有興趣者能夠自行編寫
result = html_data.xpath('//a[2][@id="link2"]/attribute::*')#獲取第二個a節點的全部屬性值
result = html_data.xpath('//p[contains(@class,"story")]/child::a[@id="link2"]')#調用child軸,能夠獲取全部子節點,而且限定獲取id=link2的a節點
result = html_data.xpath('//body/descendant::b') #獲取body子孫節點,而且只獲取子孫節點b
result = html_data.xpath('//p/following-sibling::*') #獲取當前節點以後全部的同級節點

 

 

 

 

 

 `

相關文章
相關標籤/搜索