Python爬蟲必備之XPath解析庫
一、簡(jiǎn)介
XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對(duì)元素和屬性進(jìn)行遍歷。XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 都構(gòu)建于 XPath 表達(dá)之上。
Xpath解析庫介紹:數(shù)據(jù)解析的過程中使用過正則表達(dá)式, 但正則表達(dá)式想要進(jìn)準(zhǔn)匹配難度較高, 一旦正則表達(dá)式書寫錯(cuò)誤, 匹配的數(shù)據(jù)也會(huì)出錯(cuò)。
網(wǎng)頁由三部分組成: HTML, Css, JavaScript, HTML頁面標(biāo)簽存在層級(jí)關(guān)系, 即DOM樹, 在獲取目標(biāo)數(shù)據(jù)時(shí)可以根據(jù)網(wǎng)頁層次關(guān)系定位標(biāo)簽, 在獲取標(biāo)簽的文本或?qū)傩浴?/p>
二、安裝
pip install lxml
三、節(jié)點(diǎn)
3.1 選取節(jié)點(diǎn)
XPath 使用路徑表達(dá)式在 XML 文檔中選取節(jié)點(diǎn)。節(jié)點(diǎn)是通過沿著路徑或者 step 來選取的。 下面列出了最有用的路徑表達(dá)式:
表達(dá)式 | 描述 |
---|---|
nodename | 選取此節(jié)點(diǎn)的所有子節(jié)點(diǎn)。 |
/ | 從根節(jié)點(diǎn)選取。 |
// | 從匹配選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn),而不考慮它們的位置。 |
… | 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。 |
. | 選取當(dāng)前節(jié)點(diǎn)。 |
@ | 選取屬性。 |
3.2 選取未知節(jié)點(diǎn)
XPath 通配符可用來選取未知的 XML 元素。
通配符 | 描述 |
---|---|
* | 匹配任何元素節(jié)點(diǎn)。 |
@* | 匹配任何屬性節(jié)點(diǎn)。 |
node() | 匹配任何類型的節(jié)點(diǎn)。 |
在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:
路徑表達(dá)式 | 結(jié)果 |
---|---|
/bookstore/* | 選取 bookstore 元素的所有子元素。 |
//* | 選取文檔中的所有元素。 |
//title[@*] | 選取所有帶有屬性的 title 元素。 |
3.3 節(jié)點(diǎn)關(guān)系
父(Parent)
每個(gè)元素以及屬性都有一個(gè)父。
在下面的例子中,book 元素是 title、author、year 以及 price 元素的父:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
子(Children)
元素節(jié)點(diǎn)可有零個(gè)、一個(gè)或多個(gè)子。
在下面的例子中,title、author、year 以及 price 元素都是 book 元素的子:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
同胞(Sibling)
擁有相同的父的節(jié)點(diǎn)
在下面的例子中,title、author、year 以及 price 元素都是同胞:
<book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
先輩(Ancestor)
某節(jié)點(diǎn)的父、父的父,等等。
在下面的例子中,title 元素的先輩是 book 元素和 bookstore 元素:
<bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
后代(Descendant)
某個(gè)節(jié)點(diǎn)的子,子的子,等等。
在下面的例子中,bookstore 的后代是 book、title、author、year 以及 price 元素:
<bookstore> <book> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
四、XPath實(shí)例
爬取糗事百科
import requests # 導(dǎo)包 from lxml import etree import os base_url = 'https://www.qiushibaike.com/video/' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36' } res = requests.get(url=base_url, headers=headers) html = res.content.decode('utf-8') # xpath解析 tree = etree.HTML(html) # 標(biāo)題 content = tree.xpath('//*/a/div[@class="content"]/span/text()') # 視頻 video_list = tree.xpath('//*/video[@controls="controls"]/source/@src') index = 0 for i in video_list: # 獲取視頻二進(jìn)制流 video_content = requests.get(url= 'https:' + i,headers=headers).content # 標(biāo)題 title_1 = content[0].strip('\n') # 將視頻二進(jìn)制寫入文件 with open(f'Video/{title_1}.mp4','wb') as f: f.write(video_content) index += 1
到此這篇關(guān)于Python爬蟲必備之XPath解析庫的文章就介紹到這了,更多相關(guān)XPath解析庫內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。