Python xpath表達式如何實現數據處理
xpath表達式
1. xpath語法
<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 選取節點
XPath 使用路徑表達式來選取 XML 文檔中的節點或者節點集。這些路徑表達式和我們在常規的電腦文件系統中看到的表達式非常相似。
使用chrome插件選擇標簽時候,選中時,選中的標簽會添加屬性class='xh-highlight'
下面列出了最有用的表達式:
表達式 描述 nodename 選中該元素。 / 從根節點選取、或者是元素和元素間的過渡。 // 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。 . 選取當前節點。 .. 選取當前節點的父節點。 @ 選取屬性。 text() 選取文本。
實例
路徑表達式 結果 bookstore 選擇bookstore元素。 /bookstore 選取根元素 bookstore。注釋:假如路徑起始于正斜杠( / ),則此路徑始終代表到某元素的絕對路徑! bookstore/book 選取屬于 bookstore 的子元素的所有 book 元素。 //book 選取所有 book 子元素,而不管它們在文檔中的位置。 bookstore//book 選擇屬于 bookstore 元素的后代的所有 book 元素,而不管它們位于 bookstore 之下的什么位置。 //book/title/@lang 選擇所有的book下面的title中的lang屬性的值。 //book/title/text() 選擇所有的book下面的title的文本。
選擇所有的h1下的文本 //h1/text() 獲取所有的a標簽的href //a/@href 獲取html下的head下的title的文本 /html/head/title/text() 獲取html下的head下的link標簽的href /html/head/link/@href1.2 查找特定的節點
路徑表達式 結果 //title[@lang='eng'] 選擇lang屬性值為eng的所有title元素 /bookstore/book[1] 選取屬于 bookstore 子元素的第一個 book 元素。 /bookstore/book[last()] 選取屬于 bookstore 子元素的最后一個 book 元素。 /bookstore/book[last()-1] 選取屬于 bookstore 子元素的倒數第二個 book 元素。 /bookstore/book[position()>1] 選擇bookstore下面的book元素,從第二個開始選擇 //book/title[text()=’Harry Potter’] 選擇所有book下的title元素,僅僅選擇文本為Harry Potter的title元素 /bookstore/book[price>35.00]/title 選取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值須大于 35.00。
注意點: 在xpath中,第一個元素的位置是1,最后一個元素的位置是last(),倒數第二個是last()-1
1.3 選取未知節點
XPath 通配符可用來選取未知的 XML 元素。
通配符 描述 * 匹配任何元素節點。 @* 匹配任何屬性節點。 node() 匹配任何類型的節點。
實例
在下面的表格中,我們列出了一些路徑表達式,以及這些表達式的結果:
路徑表達式 結果 /bookstore/* 選取 bookstore 元素的所有子元素。 //* 選取文檔中的所有元素。 //title[@*] 選取所有帶有屬性的 title 元素。
1.4 選取若干路徑
通過在路徑表達式中使用“|”運算符,您可以選取若干個路徑。
實例
在下面的表格中,我們列出了一些路徑表達式,以及這些表達式的結果:
路徑表達式 結果 //book/title | //book/price 選取 book 元素的所有 title 和 price 元素。 //title | //price 選取文檔中的所有 title 和 price 元素。 /bookstore/book/title | //price 選取屬于 bookstore 元素的 book 元素的所有 title 元素,以及文檔中所有的 price 元素。
實例:
from lxml import etreetext = ’’’ <div> <ul> <li class='item-1'><a href='http://www.cgvv.com.cn/bcjs/link1.html' rel='external nofollow' >first item</a></li> <li class='item-1'><a href='http://www.cgvv.com.cn/bcjs/link2.html' rel='external nofollow' >second item</a></li> <li class='item-inactive'><a href='http://www.cgvv.com.cn/bcjs/link3.html' rel='external nofollow' >third item</a></li> <li class='item-1'><a href='http://www.cgvv.com.cn/bcjs/link4.html' rel='external nofollow' >fourth item</a></li> <li class='item-0'><a href='http://www.cgvv.com.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)# 如果取到的是一個節點,返回的是element對象,可以繼續使用xpath方法,對此我們可以在后面的數據提取過程中:先根據某個標簽進行分組,分組之后再進行數據的提取li_list = html.xpath('//li[@class=’item-1’]')#在每一組中繼續進行數據的提取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)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
