亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python xpath表達(dá)式如何實(shí)現(xiàn)數(shù)據(jù)處理

瀏覽:2日期:2022-07-21 13:42:55

xpath表達(dá)式

1. xpath語(yǔ)法

<bookstore><book> <title lang='eng'>Harry Potter</title> <price>999</price></book><book> <title lang='eng'>Learning XML</title> <price>888</price></book></bookstore>

1.1 選取節(jié)點(diǎn)

XPath 使用路徑表達(dá)式來(lái)選取 XML 文檔中的節(jié)點(diǎn)或者節(jié)點(diǎn)集。這些路徑表達(dá)式和我們?cè)诔R?guī)的電腦文件系統(tǒng)中看到的表達(dá)式非常相似。

使用chrome插件選擇標(biāo)簽時(shí)候,選中時(shí),選中的標(biāo)簽會(huì)添加屬性class='xh-highlight'

下面列出了最有用的表達(dá)式:

表達(dá)式 描述 nodename 選中該元素。 / 從根節(jié)點(diǎn)選取、或者是元素和元素間的過(guò)渡。 // 從匹配選擇的當(dāng)前節(jié)點(diǎn)選擇文檔中的節(jié)點(diǎn),而不考慮它們的位置。 . 選取當(dāng)前節(jié)點(diǎn)。 .. 選取當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)。 @ 選取屬性。 text() 選取文本。

實(shí)例

路徑表達(dá)式 結(jié)果 bookstore 選擇bookstore元素。 /bookstore 選取根元素 bookstore。注釋:假如路徑起始于正斜杠( / ),則此路徑始終代表到某元素的絕對(duì)路徑! bookstore/book 選取屬于 bookstore 的子元素的所有 book 元素。 //book 選取所有 book 子元素,而不管它們?cè)谖臋n中的位置。 bookstore//book 選擇屬于 bookstore 元素的后代的所有 book 元素,而不管它們位于 bookstore 之下的什么位置。 //book/title/@lang 選擇所有的book下面的title中的lang屬性的值。 //book/title/text() 選擇所有的book下面的title的文本。

選擇所有的h1下的文本 //h1/text() 獲取所有的a標(biāo)簽的href //a/@href 獲取html下的head下的title的文本 /html/head/title/text() 獲取html下的head下的link標(biāo)簽的href /html/head/link/@href

1.2 查找特定的節(jié)點(diǎn)

路徑表達(dá)式 結(jié)果 //title[@lang='eng'] 選擇lang屬性值為eng的所有title元素 /bookstore/book[1] 選取屬于 bookstore 子元素的第一個(gè) book 元素。 /bookstore/book[last()] 選取屬于 bookstore 子元素的最后一個(gè) book 元素。 /bookstore/book[last()-1] 選取屬于 bookstore 子元素的倒數(shù)第二個(gè) book 元素。 /bookstore/book[position()>1] 選擇bookstore下面的book元素,從第二個(gè)開(kāi)始選擇 //book/title[text()=’Harry Potter’] 選擇所有book下的title元素,僅僅選擇文本為Harry Potter的title元素 /bookstore/book[price>35.00]/title 選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大于 35.00。

注意點(diǎn): 在xpath中,第一個(gè)元素的位置是1,最后一個(gè)元素的位置是last(),倒數(shù)第二個(gè)是last()-1

1.3 選取未知節(jié)點(diǎn)

XPath 通配符可用來(lái)選取未知的 XML 元素。

通配符 描述 * 匹配任何元素節(jié)點(diǎn)。 @* 匹配任何屬性節(jié)點(diǎn)。 node() 匹配任何類型的節(jié)點(diǎn)。

實(shí)例

在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:

路徑表達(dá)式 結(jié)果 /bookstore/* 選取 bookstore 元素的所有子元素。 //* 選取文檔中的所有元素。 //title[@*] 選取所有帶有屬性的 title 元素。

1.4 選取若干路徑

通過(guò)在路徑表達(dá)式中使用“|”運(yùn)算符,您可以選取若干個(gè)路徑。

實(shí)例

在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:

路徑表達(dá)式 結(jié)果 //book/title | //book/price 選取 book 元素的所有 title 和 price 元素。 //title | //price 選取文檔中的所有 title 和 price 元素。 /bookstore/book/title | //price 選取屬于 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。

實(shí)例:

from lxml import etreetext = ’’’ <div> <ul> <li class='item-1'><a href='http://www.aoyou183.cn/bcjs/link1.html' rel='external nofollow' >first item</a></li> <li class='item-1'><a href='http://www.aoyou183.cn/bcjs/link2.html' rel='external nofollow' >second item</a></li> <li class='item-inactive'><a href='http://www.aoyou183.cn/bcjs/link3.html' rel='external nofollow' >third item</a></li> <li class='item-1'><a href='http://www.aoyou183.cn/bcjs/link4.html' rel='external nofollow' >fourth item</a></li> <li class='item-0'><a href='http://www.aoyou183.cn/bcjs/link5.html' rel='external nofollow' >fifth item</a> </ul> </div> ’’’html = etree.HTML(text)#獲取href的列表和title的列表href_list = html.xpath('//li[@class=’item-1’]/a/@href')title_list = html.xpath('//li[@class=’item-1’]/a/text()')#組裝成字典for href in href_list: item = {} item['href'] = href item['title'] = title_list[href_list.index(href)] print(item)# 如果取到的是一個(gè)節(jié)點(diǎn),返回的是element對(duì)象,可以繼續(xù)使用xpath方法,對(duì)此我們可以在后面的數(shù)據(jù)提取過(guò)程中:先根據(jù)某個(gè)標(biāo)簽進(jìn)行分組,分組之后再進(jìn)行數(shù)據(jù)的提取li_list = html.xpath('//li[@class=’item-1’]')#在每一組中繼續(xù)進(jìn)行數(shù)據(jù)的提取for li in li_list: item = {} item['href'] = li.xpath('./a/@href')[0] if len(li.xpath('./a/@href'))>0 else None item['title'] = li.xpath('./a/text()')[0] if len(li.xpath('./a/text()'))>0 else None print(item)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产色婷婷精品综合在线观看 | 免费一级视频在线播放 | 1024国产视频 | 国产色婷婷精品免费视频 | 精品国产一区二区三区香蕉沈先生 | 亚洲精品tv久久久久久久久久 | 91精彩视频| 久久久精品午夜免费不卡 | 国产乱人视频免费观看 | 1000部末满18在线观看黄 | 色综合999| 91精品国产综合久久青草 | 98精品国产高清在线xxxx | 国内精品视频区在线2021 | 亚洲性久久 | www黄色com| 国产一区二区三区精品视频 | 日韩美一区二区三区 | 欧美性猛交 | 亚洲免费色图 | 亚洲一区二区三区不卡在线播放 | 狠狠色丁香婷婷久久综合2021 | www视频在线观看 | 国产在线播放拍拍拍 | 一级毛片黄片 | 亚洲国产香蕉视频欧美 | 久久久精品一级二级三级 | 欧美一级毛片高清视频 | 日本一级毛片高清免费观看视频 | 久久亚洲综合色 | 美日韩一区二区 | 国产精品白丝喷水在线观看 | 欧美成人精品第一区二区三区 | 日韩视频在线观看视频 | 日本一级在线播放线观看视频 | 海外毛片 | 一区二区国产在线观看 | 免费观看wwwwwww | 成年轻人在线看片 | 精品久久网站 | 97久久天天综合色天天综合色hd |