Python爬蟲必備之XPath解析庫
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 etreeimport osbase_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 = 0for 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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)入門第三篇(ASP腳本基礎(chǔ))2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. 解析原生JS getComputedStyle4. 前端從瀏覽器的渲染到性能優(yōu)化5. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁6. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)7. ASP實(shí)現(xiàn)加法驗(yàn)證碼8. 讀大數(shù)據(jù)量的XML文件的讀取問題9. css代碼優(yōu)化的12個(gè)技巧10. 利用CSS3新特性創(chuàng)建透明邊框三角
