python爬蟲之xpath的基本使用詳解
一、簡介
XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對(duì)元素和屬性進(jìn)行遍歷。XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 都構(gòu)建于 XPath 表達(dá)之上。
二、安裝
pip3 install lxml
三、使用
1、導(dǎo)入
from lxml import etree
2、基本使用
from lxml import etree wb_data = """ <div> <ul> <li class="item-0"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li> <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li> <li class="item-inactive"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li> <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li> <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a> </ul> </div> """ html = etree.HTML(wb_data) print(html) result = etree.tostring(html) print(result.decode("utf-8"))
從下面的結(jié)果來看,我們打印機(jī)html其實(shí)就是一個(gè)python對(duì)象,etree.tostring(html)則是不全里html的基本寫法,補(bǔ)全了缺胳膊少腿的標(biāo)簽。
<Element html at 0x39e58f0> <html><body><div> <ul> <li class="item-0"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li> <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li> <li class="item-inactive"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li> <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li> <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a> </li></ul> </div> </body></html>
3、獲取某個(gè)標(biāo)簽的內(nèi)容(基本使用),注意,獲取a標(biāo)簽的所有內(nèi)容,a后面就不用再加正斜杠,否則報(bào)錯(cuò)。
寫法一
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a') print(html) for i in html_data: print(i.text) <Element html at 0x12fe4b8> first item second item third item fourth item fifth item
寫法二(直接在需要查找內(nèi)容的標(biāo)簽后面加一個(gè)/text()就行)
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a/text()') print(html) for i in html_data: print(i) <Element html at 0x138e4b8> first item second item third item fourth item fifth item
4、打開讀取html文件
#使用parse打開html的文件 html = etree.parse('test.html') html_data = html.xpath('//*')<br>#打印是一個(gè)列表,需要遍歷 print(html_data) for i in html_data: print(i.text)
html = etree.parse('test.html') html_data = etree.tostring(html,pretty_print=True) res = html_data.decode('utf-8') print(res) 打?。? <div> <ul> <li class="item-0"><a href="link1.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >first item</a></li> <li class="item-1"><a href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >second item</a></li> <li class="item-inactive"><a href="link3.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >third item</a></li> <li class="item-1"><a href="link4.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fourth item</a></li> <li class="item-0"><a href="link5.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" >fifth item</a></li> </ul> </div>
5、打印指定路徑下a標(biāo)簽的屬性(可以通過遍歷拿到某個(gè)屬性的值,查找標(biāo)簽的內(nèi)容)
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a/@href') for i in html_data: print(i)
打印:
link1.html
link2.html
link3.html
link4.html
link5.html
6、我們知道我們使用xpath拿到得都是一個(gè)個(gè)的ElementTree對(duì)象,所以如果需要查找內(nèi)容的話,還需要遍歷拿到數(shù)據(jù)的列表。
查到絕對(duì)路徑下a標(biāo)簽屬性等于link2.html的內(nèi)容。
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a[@href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]/text()') print(html_data) for i in html_data: print(i)
打印:
['second item']
second item
7、上面我們找到全部都是絕對(duì)路徑(每一個(gè)都是從根開始查找),下面我們查找相對(duì)路徑,例如,查找所有l(wèi)i標(biāo)簽下的a標(biāo)簽內(nèi)容。
html = etree.HTML(wb_data) html_data = html.xpath('//li/a/text()') print(html_data) for i in html_data: print(i)
打?。?/p>
['first item', 'second item', 'third item', 'fourth item', 'fifth item']
first item
second item
third item
fourth item
fifth item
8、上面我們使用絕對(duì)路徑,查找了所有a標(biāo)簽的屬性等于href屬性值,利用的是/---絕對(duì)路徑,下面我們使用相對(duì)路徑,查找一下l相對(duì)路徑下li標(biāo)簽下的a標(biāo)簽下的href屬性的值,注意,a標(biāo)簽后面需要雙//。
html = etree.HTML(wb_data) html_data = html.xpath('//li/a//@href') print(html_data) for i in html_data: print(i)
打?。?/p>
['link1.html', 'link2.html', 'link3.html', 'link4.html', 'link5.html']
link1.html
link2.html
link3.html
link4.html
link5.html
9、相對(duì)路徑下跟絕對(duì)路徑下查特定屬性的方法類似,也可以說相同。
html = etree.HTML(wb_data) html_data = html.xpath('//li/a[@href="link2.html" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]') print(html_data) for i in html_data: print(i.text)
打印:
[<Element a at 0x216e468>]
second item
10、查找最后一個(gè)li標(biāo)簽里的a標(biāo)簽的href屬性
html = etree.HTML(wb_data) html_data = html.xpath('//li[last()]/a/text()') print(html_data) for i in html_data: print(i)
打?。?/p>
['fifth item']
fifth item
11、查找倒數(shù)第二個(gè)li標(biāo)簽里的a標(biāo)簽的href屬性
html = etree.HTML(wb_data) html_data = html.xpath('//li[last()-1]/a/text()') print(html_data) for i in html_data: print(i)
打印:
['fourth item']
fourth item
12、如果在提取某個(gè)頁面的某個(gè)標(biāo)簽的xpath路徑的話,可以如下圖:
//*[@id="kw"]
解釋:使用相對(duì)路徑查找所有的標(biāo)簽,屬性id等于kw的標(biāo)簽。
常用
#!/usr/bin/env python # -*- coding:utf-8 -*- from scrapy.selector import Selector, HtmlXPathSelector from scrapy.http import HtmlResponse html = """<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <ul> <li class="item-"><a id='i1' href="link.html" rel="external nofollow" rel="external nofollow" >first item</a></li> <li class="item-0"><a id='i2' href="llink.html" rel="external nofollow" >first item</a></li> <li class="item-1"><a href="llink2.html" rel="external nofollow" rel="external nofollow" >second item<span>vv</span></a></li> </ul> <div><a href="llink2.html" rel="external nofollow" rel="external nofollow" >second item</a></div> </body> </html> """ response = HtmlResponse(url='http://example.com', body=html,encoding='utf-8') # hxs = HtmlXPathSelector(response) # print(hxs) # hxs = Selector(response=response).xpath('//a') # print(hxs) # hxs = Selector(response=response).xpath('//a[2]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@id]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@id="i1"]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@href="link.html" rel="external nofollow" rel="external nofollow" ][@id="i1"]') # print(hxs) # hxs = Selector(response=response).xpath('//a[contains(@href, "link")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[starts-with(@href, "link")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]/text()').extract() # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]/@href').extract() # print(hxs) # hxs = Selector(response=response).xpath('/html/body/ul/li/a/@href').extract() # print(hxs) # hxs = Selector(response=response).xpath('//body/ul/li/a/@href').extract_first() # print(hxs) # ul_list = Selector(response=response).xpath('//body/ul/li') # for item in ul_list: # v = item.xpath('./a/span') # # 或 # # v = item.xpath('a/span') # # 或 # # v = item.xpath('*/a/span') # print(v)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python創(chuàng)建websocket服務(wù)端并給出不同客戶端的請求
本文主要介紹了使用Python創(chuàng)建websocket服務(wù)端并給出不同客戶端的請求,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01python 實(shí)現(xiàn)分組求和與分組累加求和代碼
這篇文章主要介紹了python 實(shí)現(xiàn)分組求和與分組累加求和代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05詳解python模塊pychartdir安裝及導(dǎo)入問題
這篇文章主要介紹了python模塊pychartdir導(dǎo)入問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10pandas數(shù)據(jù)框,統(tǒng)計(jì)某列數(shù)據(jù)對(duì)應(yīng)的個(gè)數(shù)方法
下面小編就為大家分享一篇pandas數(shù)據(jù)框,統(tǒng)計(jì)某列數(shù)據(jù)對(duì)應(yīng)的個(gè)數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python高級(jí)應(yīng)用探索之元編程和并發(fā)編程詳解
Python作為一種簡單易用且功能強(qiáng)大的編程語言,廣泛應(yīng)用于各個(gè)領(lǐng)域,本文主要來和大家一起探索一下Python中的優(yōu)化技巧、元編程和并發(fā)編程,希望對(duì)大家有所幫助2023-11-11Python命令啟動(dòng)Web服務(wù)器實(shí)例詳解
這篇文章主要介紹了Python命令啟動(dòng)Web服務(wù)器實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02解決ImportError: cannot import name ‘Imput
您遇到的ImportError: cannot import name ‘Imputer‘錯(cuò)誤提示表明您嘗試導(dǎo)入一個(gè)名為’Imputer’的模塊或類,但是該模塊或類無法找到,本文小編給大家介紹了如何解決這個(gè)問題,需要的朋友可以參考下2023-10-10