快速入門(mén)XPath語(yǔ)法,輕松解析爬蟲(chóng)時(shí)的HTML內(nèi)容
HTML、XML、XPath簡(jiǎn)介
HTML是Hyper Text Markup Language(超文本標(biāo)記語(yǔ)言)的縮寫(xiě),我們?cè)跒g覽器中看到的內(nèi)容都是HTML代碼經(jīng)過(guò)瀏覽器渲染的結(jié)果。
XML是EXtensible Markup Language(可擴(kuò)展標(biāo)記語(yǔ)言)的縮寫(xiě),XML是一種很類(lèi)似HTML的標(biāo)記語(yǔ)言,不過(guò)XML的設(shè)計(jì)宗旨是傳輸數(shù)據(jù),而非顯示數(shù)據(jù)。
XPath是XML Path Language(XML路徑語(yǔ)言)的縮寫(xiě),是一門(mén)在XML文檔中查找信息的語(yǔ)言,用來(lái)提取XML文檔中的元素和屬性。
XPath語(yǔ)法介紹
路徑表達(dá)式:
nodename 選取此節(jié)點(diǎn)的所有子節(jié)點(diǎn)。
/ 從根節(jié)點(diǎn)選取。正斜杠也是路徑分隔符。
// 從任意位置選取文檔中的節(jié)點(diǎn)。
. 選取當(dāng)前節(jié)點(diǎn)。
.. 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。
@ 選取當(dāng)前節(jié)點(diǎn)的屬性
通配符:
* 任意元素。
@* 任意屬性。
node() 任意子節(jié)點(diǎn)(元素,屬性,內(nèi)容)。
謂語(yǔ):
//a[n] n為1開(kāi)始的整數(shù),選取排在第n個(gè)位置的<a>元素。
//a[last()] last()表示選取排在最后位置的<a>元素。
//a[last()-1] 和上面同理,表示選取倒數(shù)第二個(gè)<a>元素。
//a[position()<3] 選取第一個(gè)和第二個(gè)<a>元素。
//a[@href] 選取擁有href屬性的<a>元素。
//a[@href='www.baidu.com'] 選取href屬性值為'www.baidu.com'的<a>元素。
//a[@price>10] 選取price屬性值大于10的<a>元素。
//a[@price>10]/span 選取price屬性值大于10的<a>元素下的<span>元素。
選取多個(gè)路徑:
//book/title | //book/price 選取<book>元素的所有<title>和<price>元素。
//title | //price 選取所有<title>和<price>元素。
/bookstore/book/title | //price 選取屬于<bookstore>元素的<book>元素的所有<title>元素,以及所有的<price>元素。
運(yùn)算符:
+ - * div 加減乘除。
= != 等于,不等于。
< <= 小于,小于等于。
> >= 大于,大于等于。
or and 或,與
mod 計(jì)算余數(shù)
常用函數(shù):
contains(@屬性,string) 選取屬性里包含字符串string的元素。
text() 獲取元素中的內(nèi)容。
last() 選取最后一個(gè)元素。
position() 用于選取多個(gè)元素中某些位置(數(shù)字編號(hào))的元素。
count() 返回元素的數(shù)量。
max() 返回最大的元素,min(),avg(),sum()同理。
lxml庫(kù)使用
安裝命令:
pip install lxml
實(shí)戰(zhàn)演練:
# coding=utf-8
from lxml import etree
text = '''
<!DOCTYPE html>
<html>
<head>
<title>XPath Test(公眾號(hào):小斌哥ge)</title>
</head>
<body>
<div>
<ul>
<li id="1" class="item"><a href="link1.html">first item</a></li>
<li id="2" class="item"><a href="link2.html">second item</a></li>
<li id="3" class="item-inactive"><a href="link3.html">third item</a></li>
<li id="4" class="project"><a href="link4.html">first project</a></li>
<li id="5" class="project"><a href="link5.html">second project</a></li>
</ul>
</div>
</body>
</html>
'''
# 利用etree.HTML,將字符串解析為HTML文檔
html = etree.HTML(text)
# 讀取html文件
# html = etree.parse('test.html')
print(html)
# 按字符串序列化HTML文檔
result = etree.tostring(html, pretty_print=True)
print(result)<Element html at 0x1c3be4c9740>
b'<html>\n
<head>\n<title>XPath Test</title>\n</head>\n
<body>\n<div>\n
<ul>\n
<li id="1" class="item"><a href="link1.html">first item</a></li>\n
<li id="2" class="item"><a href="link2.html">second item</a></li>\n
<li id="3" class="item-inactive"><a href="link3.html">third item</a></li>\n
<li id="4" class="project"><a href="link4.html">first project</a></li>\n
<li id="5" class="project"><a href="link5.html">second project</a></li>\n
</ul>\n
</div>\n</body>\n
</html>\n'用XPath從解析器中提取數(shù)據(jù):
# 獲取所有class屬性值為'item'的<li>元素
infos = html.xpath("http://li[@class='item']")
print(infos)[<Element li at 0x25951272840>, <Element li at 0x25951272780>]# 獲取所有class屬性中包含'pro'字符串的<li>元素
infos = html.xpath("http://li[contains(@class, 'pro')]")
for info in infos:
# 獲取<li>元素中<a>元素的文本內(nèi)容
print(info.xpath("a/text()"))['first project']
['second project']# 獲取倒數(shù)第二個(gè)<li>元素下的最后一個(gè)<a>元素
info = html.xpath("http://li[last()-1]/a[last()]")
# 打印<a>元素的內(nèi)容
print(info[0].text)
first project# 獲取前三個(gè)<li>元素下的<a>元素的內(nèi)容
infos = html.xpath("http://li[position()<4]/a/text()")
for info in infos:
# 打印<a>元素的文本內(nèi)容
print(info)
first item
second item
third item參考文檔:
[1] XML W3School官方文檔:http://www.w3school.com.cn/xml/index.asp
[2] XPath W3School官方文檔:http://www.w3school.com.cn/xpath/index.asp
[3] lxml Python官方文檔:http://lxml.de/index.html
評(píng)論
圖片
表情
