python爬虫(8)--Xpath语法与lxml库
?
1.XPath語法
XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進(jìn)行遍歷。XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 都構(gòu)建于 XPath 表達(dá)之上。
節(jié)點(diǎn)關(guān)系
(1)父(Parent)
每個(gè)元素以及屬性都有一個(gè)父。
在下面的例子中,book 元素是 title、author、year 以及 price 元素的父:
<book><title>family</title><year>2007</year><price>78</price> </book>(2)子(Children)
元素節(jié)點(diǎn)可有零個(gè)、一個(gè)或多個(gè)子。
在上面的例子中,title、author、year 以及 price 元素都是 book 元素的子
(3)同胞(Sibling)
擁有相同的父的節(jié)點(diǎn)
在上面的例子中,title、author、year 以及 price 元素都是同胞
(4)先輩(Ancestor)
某節(jié)點(diǎn)的父、父的父,等等。
在下面的例子中,title 元素的先輩是 book 元素和 bookstore 元素
<bookstore> <book><title>family</title><year>2007</year><price>78</price> </book> </bookstore>(5)后代(Descendant)
某個(gè)節(jié)點(diǎn)的子,子的子,等等。
在下面的例子中,bookstore 的后代是 book、title、author、year 以及 price 元素
選取節(jié)點(diǎn)
XPath 使用路徑表達(dá)式在 XML 文檔中選取節(jié)點(diǎn)。節(jié)點(diǎn)是通過沿著路徑或者 step 來選取的。
下面列出了最有用的路徑表達(dá)式:
實(shí)例
在下面的表格中,我們已列出了一些路徑表達(dá)式以及表達(dá)式的結(jié)果:
謂語(Predicates)
謂語用來查找某個(gè)特定的節(jié)點(diǎn)或者包含某個(gè)指定的值的節(jié)點(diǎn)。
謂語被嵌在方括號中。
實(shí)例
在下面的表格中,我們列出了帶有謂語的一些路徑表達(dá)式,以及表達(dá)式的結(jié)果:
選取未知節(jié)點(diǎn)
XPath 通配符可用來選取未知的 XML 元素。
實(shí)例
在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:
選取若干路徑
通過在路徑表達(dá)式中使用“|”運(yùn)算符,您可以選取若干個(gè)路徑。
實(shí)例
在下面的表格中,我們列出了一些路徑表達(dá)式,以及這些表達(dá)式的結(jié)果:
XPath 運(yùn)算符
下面列出了可用在 XPath 表達(dá)式中的運(yùn)算符:
| | | 計(jì)算兩個(gè)節(jié)點(diǎn)集 | //book | //cd | 返回所有擁有 book 和 cd 元素的節(jié)點(diǎn)集 |
| + | 加法 | 6 + 4 | 10 |
| – | 減法 | 6 – 4 | 2 |
| * | 乘法 | 6 * 4 | 24 |
| div | 除法 | 8 div 4 | 2 |
| = | 等于 | price=9.80 | 如果 price 是 9.80,則返回 true。如果 price 是 9.90,則返回 false。 |
| != | 不等于 | price!=9.80 | 如果 price 是 9.90,則返回 true。如果 price 是 9.80,則返回 false。 |
| < | 小于 | price<9.80 | 如果 price 是 9.00,則返回 true。如果 price 是 9.90,則返回 false。 |
| <= | 小于或等于 | price<=9.80 | 如果 price 是 9.00,則返回 true。如果 price 是 9.90,則返回 false。 |
| > | 大于 | price>9.80 | 如果 price 是 9.90,則返回 true。如果 price 是 9.80,則返回 false。 |
| >= | 大于或等于 | price>=9.80 | 如果 price 是 9.90,則返回 true。如果 price 是 9.70,則返回 false。 |
| or | 或 | price=9.80 or price=9.70 | 如果 price 是 9.80,則返回 true。如果 price 是 9.50,則返回 false。 |
| and | 與 | price>9.00 and price<9.90 | 如果 price 是 9.80,則返回 true。如果 price 是 8.50,則返回 false。 |
| mod | 計(jì)算除法的余數(shù) | 5 mod 2 | 1 |
2.lxml用法
初步使用
首先我們利用它來解析 HTML 代碼,先來一個(gè)小例子來感受一下它的基本用法。
from lxml import etreetext=''' <div><ul><li class="item-0"><a href="link1.html">first item</a></li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-inactive"><a href="link3.html">third item</a></li><li class="item-1"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></ul> </div> ''' html = etree.HTML(text) result = etree.tostring(html) print result首先使用 lxml 的 etree 庫,然后利用 etree.HTML 初始化,然后我們將其打印出來。
其中,這里體現(xiàn)了 lxml 的一個(gè)非常實(shí)用的功能就是自動修正 html 代碼,應(yīng)該注意到了,最后一個(gè) li 標(biāo)簽,其實(shí)把尾標(biāo)簽刪掉了,是不閉合的。不過,lxml 因?yàn)槔^承了 libxml2 的特性,具有自動修正 HTML 代碼的功能。
所以輸出結(jié)果是這樣的
<html><body><div><ul><li class="item-0"><a href="link1.html">first item</a></li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-inactive"><a href="link3.html">third item</a></li><li class="item-1"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></li></ul> </div> </body></html>不僅補(bǔ)全了 li 標(biāo)簽,還添加了 body,html 標(biāo)簽。
文件讀取
除了直接讀取字符串,還支持從文件讀取內(nèi)容。比如我們新建一個(gè)文件叫做 hello.html,內(nèi)容為上面的text
html = etree.parse('hello.html') result = etree.tostring(html) print result利用 parse 方法來讀取文件,同樣可以得到相同的結(jié)果。
XPath實(shí)例測試
依然以上一段程序?yàn)槔?/p>
(1)獲取所有的 <li> 標(biāo)簽
html = etree.parse('hello.html') print type(html) result = html.xpath('//li') print result print len(result) print type(result) print type(result[0])運(yùn)行結(jié)果
<type 'lxml.etree._ElementTree'> [<Element li at 0x312d558>, <Element li at 0x312d530>, <Element li at 0x312d508>, <Element li at 0x312d2b0>, <Element li at 0x312d0d0>] 5 <type 'list'> <type 'lxml.etree._Element'>可見,etree.parse 的類型是 ElementTree,通過調(diào)用 xpath 以后,得到了一個(gè)列表,包含了 5 個(gè) <li> 元素,每個(gè)元素都是 Element 類型
(2)獲取 <li> 標(biāo)簽的所有 class
html = etree.parse('hello.html') print type(html) result = html.xpath('//li/@class') print result運(yùn)行結(jié)果
<type 'lxml.etree._ElementTree'> ['item-0', 'item-1', 'item-inactive', 'item-1', 'item-0'](3)獲取 <li> 標(biāo)簽下 href 為 link1.html 的 <a> 標(biāo)簽
html = etree.parse('hello.html') result = html.xpath('//li/a[@href="link1.html"]') print result(4)獲取 <li> 標(biāo)簽下的所有 <span> 標(biāo)簽
?/ 是用來獲取子元素的,而 <span> 并不是 <li> 的子元素,所以,要用雙斜杠
result = html.xpath('//li//span')(5)獲取 <li> 標(biāo)簽下的所有 class,不包括 <li>
result = html.xpath('//li/a//@class')(6)獲取最后一個(gè) <li> 的 <a> 的 href
result = html.xpath('//li[last()]/a/@href') #['link5.html'](7)獲取倒數(shù)第二個(gè)元素的內(nèi)容
result = html.xpath('//li[last()-1]/a')(8)獲取 class 為 item-1 的標(biāo)簽名
result = html.xpath('//*[@class="item-1"]') print result[0].tag?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lzhc/p/8302235.html
總結(jié)
以上是生活随笔為你收集整理的python爬虫(8)--Xpath语法与lxml库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二阶段冲刺10天 第六天
- 下一篇: vue小demo易错点总结