python 網(wǎng)頁解析器掌握第三方 lxml 擴(kuò)展庫(kù)與 xpath 的使用方法
今天說的則是使用另外一種擴(kuò)展庫(kù) lxml 來對(duì)網(wǎng)頁完成解析。同樣的,lxml 庫(kù)能完成對(duì) html、xml 格式的文件解析,并且能夠用來解析大型的文檔、解析速度也是相對(duì)比較快的。
要掌握 lxml 的使用,就需要掌握掌握 xpath 的使用方法,因?yàn)?lxml 擴(kuò)展庫(kù)就是基于 xpath 的,所以這一章的重點(diǎn)主要還是對(duì) xpath 語法使用的說明。
1、導(dǎo)入 lxml 擴(kuò)展庫(kù)、并創(chuàng)建對(duì)象# -*- coding: UTF-8 -*-# 從 lxml 導(dǎo)入 etreefrom lxml import etree# 首先獲取到網(wǎng)頁下載器已經(jīng)下載到的網(wǎng)頁源代碼# 這里直接取官方的案例html_doc = '''<html><head><title>The Dormouse’s story</title></head><body><p class='title'><b>The Dormouse’s story</b></p><p class='story'>Once upon a time there were three little sisters; and their names were<a rel='external nofollow' id='link1'>Elsie</a>,<a rel='external nofollow' id='link2'>Lacie</a> and<a rel='external nofollow' id='link3'>Tillie</a>;and they lived at the bottom of a well.</p><p class='story'>...</p>'''# 初始化網(wǎng)頁下載器的 html_doc 字符串,返回一個(gè) lxml 的對(duì)象html = etree.HTML(html_doc)2、使用 xpath 語法提取網(wǎng)頁元素
按照節(jié)點(diǎn)的方式獲取元素
# xpath() 使用標(biāo)簽節(jié)點(diǎn)的方式獲取元素print html.xpath(’/html/body/p’)# [<Element p at 0x2ebc908>, <Element p at 0x2ebc8c8>, <Element p at 0x2eb9a48>]print html.xpath(’/html’)# [<Element html at 0x34bc948>]# 在當(dāng)前節(jié)點(diǎn)的子孫節(jié)點(diǎn)中查找 a 節(jié)點(diǎn)print html.xpath(’//a’)# 在當(dāng)前節(jié)點(diǎn)的子節(jié)點(diǎn)中查找 html 節(jié)點(diǎn)print html.xpath(’/html’)
按照篩選的方式獲取元素
’’’根據(jù)單一屬性獲取元素’’’# 獲取子孫節(jié)點(diǎn)中,屬性 class=bro 的 a 標(biāo)簽print html.xpath(’//a[@class='bro']’)# 獲取子孫節(jié)點(diǎn)中,屬性 id=link3 的 a 標(biāo)簽print html.xpath(’//a[@id='link3']’)’’’根據(jù)多個(gè)屬性獲取元素’’’# 獲取class屬性等于sister,并且id等于link3的a標(biāo)簽print html.xpath(’//a[contains(@class,'sister') and contains(@id,'link1')]’)# 獲取class屬性等于bro,或者id等于link1的a標(biāo)簽print html.xpath(’//a[contains(@class,'bro') or contains(@id,'link1')]’)# 使用 last() 函數(shù),獲取子孫代的a標(biāo)簽的最后一個(gè)a標(biāo)簽print html.xpath(’//a[last()]’)# 使用 1 函數(shù),獲取子孫代的a標(biāo)簽的第一個(gè)a標(biāo)簽print html.xpath(’//a[1]’)# 標(biāo)簽篩選,position()獲取子孫代的a標(biāo)簽的前兩個(gè)a標(biāo)簽print html.xpath(’//a[position() < 3]’)’’’使用計(jì)算的方式,獲取多個(gè)元素’’’# 標(biāo)簽篩選,position()獲取子孫代的a標(biāo)簽的第一個(gè)與第三個(gè)標(biāo)簽# 可以使用的計(jì)算表達(dá)式:>、<、=、>=、<=、+、-、and、orprint html.xpath(’//a[position() = 1 or position() = 3]’)
獲取元素的屬性與文本
’’’使用@獲取屬性值,使用text() 獲取標(biāo)簽文本’’’# 獲取屬性值print html.xpath(’//a[position() = 1]/@class’)# [’sister’]# 獲取標(biāo)簽的文本值print html.xpath(’//a[position() = 1]/text()’)
到此這篇關(guān)于python 網(wǎng)頁解析器掌握第三方 lxml 擴(kuò)展庫(kù)與 xpath 的使用方法的文章就介紹到這了,更多相關(guān)python lxml 擴(kuò)展庫(kù)與 xpath內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python web框架的總結(jié)2. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式3. Python如何進(jìn)行時(shí)間處理4. python使用ctypes庫(kù)調(diào)用DLL動(dòng)態(tài)鏈接庫(kù)5. 詳解Python模塊化編程與裝飾器6. Python基于pyjnius庫(kù)實(shí)現(xiàn)訪問java類7. Python使用shutil模塊實(shí)現(xiàn)文件拷貝8. Python實(shí)現(xiàn)迪杰斯特拉算法過程解析9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python裝飾器三種裝飾模式的簡(jiǎn)單分析
