Python爬蟲必備之XPath解析庫(kù)
XPath 是一門在 XML 文檔中查找信息的語(yǔ)言。XPath 可用來(lái)在 XML 文檔中對(duì)元素和屬性進(jìn)行遍歷。XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 都構(gòu)建于 XPath 表達(dá)之上。
Xpath解析庫(kù)介紹:數(shù)據(jù)解析的過(guò)程中使用過(guò)正則表達(dá)式, 但正則表達(dá)式想要進(jìn)準(zhǔn)匹配難度較高, 一旦正則表達(dá)式書寫錯(cuò)誤, 匹配的數(shù)據(jù)也會(huì)出錯(cuò)。
網(wǎng)頁(yè)由三部分組成: HTML, Css, JavaScript, HTML頁(yè)面標(biāo)簽存在層級(jí)關(guān)系, 即DOM樹, 在獲取目標(biāo)數(shù)據(jù)時(shí)可以根據(jù)網(wǎng)頁(yè)層次關(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)是通過(guò)沿著路徑或者 step 來(lái)選取的。 下面列出了最有用的路徑表達(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 通配符可用來(lái)選取未知的 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解析庫(kù)的文章就介紹到這了,更多相關(guān)XPath解析庫(kù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 用xslt+css讓RSS顯示的跟網(wǎng)頁(yè)一樣漂亮2. ASP.NET MVC把數(shù)據(jù)庫(kù)中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字3. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)4. 移動(dòng)端HTML5實(shí)現(xiàn)拍照功能的兩種方法5. 測(cè)試模式 - XSL教程 - 56. ASP.NET Core自定義中間件的方式詳解7. html5手機(jī)觸屏touch事件介紹8. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效9. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例10. 教你JS更簡(jiǎn)單的獲取表單中數(shù)據(jù)(formdata)
