前面咱們介紹了 BeautifulSoup 的用法,這個已是很是強大的庫了,不過還有一些比較流行的解析庫,例如 lxml,使用的是 Xpath 語法,一樣是效率比較高的解析方法。若是你們對 BeautifulSoup 使用不太習慣的話,能夠嘗試下 Xpath。html
lxml用法源自 lxml python 官方文檔,更多內容請直接參閱官方文檔,本文對其進行翻譯與整理。node
lxmlpython
XPath語法參考 w3schoolgit
w3schoolgithub
pypi下載地址:https://pypi.python.org/pypi/lxml/3.4.2#downloadsapp
1
|
pip install lxml
|
利用 pip 安裝便可框架
XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進行遍歷。XPath 是 W3C XSLT 標準的主要元素,而且 XQuery 和 XPointer 都構建於 XPath 表達之上。scrapy
(1)父(Parent)學習
每一個元素以及屬性都有一個父。測試
在下面的例子中,book 元素是 title、author、year 以及 price 元素的父:
1
2
3
4
5
6
|
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
|
(2)子(Children)
元素節點可有零個、一個或多個子。
在下面的例子中,title、author、year 以及 price 元素都是 book 元素的子:
1
2
3
4
5
6
|
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
|
(3)同胞(Sibling)
擁有相同的父的節點
在下面的例子中,title、author、year 以及 price 元素都是同胞:
1
2
3
4
5
6
|
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
|
(4)先輩(Ancestor)
某節點的父、父的父,等等。
在下面的例子中,title 元素的先輩是 book 元素和 bookstore 元素:
1
2
3
4
5
6
7
8
9
10
|
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
|
(5)後代(Descendant)
某個節點的子,子的子,等等。
在下面的例子中,bookstore 的後代是 book、title、author、year 以及 price 元素:
1
2
3
4
5
6
7
8
9
10
|
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
|
XPath 使用路徑表達式在 XML 文檔中選取節點。節點是經過沿着路徑或者 step 來選取的。
表達式 | 描述 |
---|---|
nodename | 選取此節點的全部子節點。 |
/ | 從根節點選取。 |
// | 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置。 |
. | 選取當前節點。 |
.. | 選取當前節點的父節點。 |
@ | 選取屬性。 |
實例
在下面的表格中,咱們已列出了一些路徑表達式以及表達式的結果:
路徑表達式 | 結果 |
---|---|
bookstore | 選取 bookstore 元素的全部子節點。 |
/bookstore | 選取根元素 bookstore。註釋:假如路徑起始於正斜槓( / ),則此路徑始終表明到某元素的絕對路徑! |
bookstore/book | 選取屬於 bookstore 的子元素的全部 book 元素。 |
//book | 選取全部 book 子元素,而無論它們在文檔中的位置。 |
bookstore//book | 選擇屬於 bookstore 元素的後代的全部 book 元素,而無論它們位於 bookstore 之下的什麼位置。 |
//@lang | 選取名爲 lang 的全部屬性。 |
謂語用來查找某個特定的節點或者包含某個指定的值的節點。
謂語被嵌在方括號中。
實例
在下面的表格中,咱們列出了帶有謂語的一些路徑表達式,以及表達式的結果:
路徑表達式 | 結果 |
---|---|
/bookstore/book[1] | 選取屬於 bookstore 子元素的第一個 book 元素。 |
/bookstore/book[last()] | 選取屬於 bookstore 子元素的最後一個 book 元素。 |
/bookstore/book[last()-1] | 選取屬於 bookstore 子元素的倒數第二個 book 元素。 |
/bookstore/book[position()<3] | 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。 |
//title[@lang] | 選取全部擁有名爲 lang 的屬性的 title 元素。 |
//title[@lang=’eng’] | 選取全部 title 元素,且這些元素擁有值爲 eng 的 lang 屬性。 |
/bookstore/book[price>35.00] | 選取 bookstore 元素的全部 book 元素,且其中的 price 元素的值須大於 35.00。 |
/bookstore/book[price>35.00]/title | 選取 bookstore 元素中的 book 元素的全部 title 元素,且其中的 price 元素的值須大於 35.00。 |
XPath 通配符可用來選取未知的 XML 元素。
通配符 | 描述 |
---|---|
* | 匹配任何元素節點。 |
@* | 匹配任何屬性節點。 |
node() | 匹配任何類型的節點。 |
實例
在下面的表格中,咱們列出了一些路徑表達式,以及這些表達式的結果:
路徑表達式 | 結果 |
---|---|
/bookstore/* | 選取 bookstore 元素的全部子元素。 |
//* | 選取文檔中的全部元素。 |
//title[@*] | 選取全部帶有屬性的 title 元素。 |
經過在路徑表達式中使用「|」運算符,您能夠選取若干個路徑。
實例
在下面的表格中,咱們列出了一些路徑表達式,以及這些表達式的結果:
路徑表達式 | 結果 |
---|---|
//book/title | //book/price | 選取 book 元素的全部 title 和 price 元素。 |
//title | //price | 選取文檔中的全部 title 和 price 元素。 |
/bookstore/book/title | //price | 選取屬於 bookstore 元素的 book 元素的全部 title 元素,以及文檔中全部的 price 元素。 |
下面列出了可用在 XPath 表達式中的運算符:
運算符 | 描述 | 實例 | 返回值 |
---|---|---|---|
| | 計算兩個節點集 | //book | //cd | 返回全部擁有 book 和 cd 元素的節點集 |
+ | 加法 | 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 | 計算除法的餘數 | 5 mod 2 | 1 |
首先咱們利用它來解析 HTML 代碼,先來一個小例子來感覺一下它的基本用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from lxml import etree
text = '''
<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 初始化,而後咱們將其打印出來。
其中,這裏體現了 lxml 的一個很是實用的功能就是自動修正 html 代碼,你們應該注意到了,最後一個 li 標籤,其實我把尾標籤刪掉了,是不閉合的。不過,lxml 由於繼承了 libxml2 的特性,具備自動修正 HTML 代碼的功能。
因此輸出結果是這樣的
1
2
3
4
5
6
7
8
9
10
11
12
|
<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>
|
不只補全了 li 標籤,還添加了 body,html 標籤。
除了直接讀取字符串,還支持從文件讀取內容。好比咱們新建一個文件叫作 hello.html,內容爲
1
2
3
4
5
6
7
8
9
|
<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"><span class="bold">third item</span></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>
|
利用 parse 方法來讀取文件。
1
2
3
4
|
from lxml import etree
html = etree.parse('hello.html')
result = etree.tostring(html, pretty_print=True)
print(result)
|
一樣能夠獲得相同的結果。
依然以上一段程序爲例
(1)獲取全部的 <li> 標籤
1
2
3
4
5
6
7
8
|
from lxml import etree
html = etree.parse('hello.html')
print type(html)
result = html.xpath('//li')
print result
print len(result)
print type(result)
print type(result[0])
|
運行結果
1
2
3
4
5
|
<type 'lxml.etree._ElementTree'>
[<Element li at 0x1014e0e18>, <Element li at 0x1014e0ef0>, <Element li at 0x1014e0f38>, <Element li at 0x1014e0f80>, <Element li at 0x1014e0fc8>]
5
<type 'list'>
<type 'lxml.etree._Element'>
|
可見,etree.parse 的類型是 ElementTree,經過調用 xpath 之後,獲得了一個列表,包含了 5 個 <li> 元素,每一個元素都是 Element 類型
(2)獲取 <li> 標籤的全部 class
1
2
|
result = html.xpath('//li/@class')
print result
|
運行結果
1
|
['item-0', 'item-1', 'item-inactive', 'item-1', 'item-0']
|
(3)獲取 <li> 標籤下 href 爲 link1.html 的 <a> 標籤
1
2
|
result = html.xpath('//li/a[@href="link1.html"]')
print result
|
運行結果
1
|
[<Element a at 0x10ffaae18>]
|
(4)獲取 <li> 標籤下的全部 <span> 標籤
注意這麼寫是不對的
1
|
result = html.xpath('//li/span')
|
由於 / 是用來獲取子元素的,而 <span> 並非 <li> 的子元素,因此,要用雙斜槓
1
2
|
result = html.xpath('//li//span')
print result
|
運行結果
1
|
[<Element span at 0x10d698e18>]
|
(5)獲取 <li> 標籤下的全部 class,不包括 <li>
1
2
|
result = html.xpath('//li/a//@class')
print result
|
運行結果
1
|
['blod']
|
(6)獲取最後一個 <li> 的 <a> 的 href
1
2
|
result = html.xpath('//li[last()]/a/@href')
print result
|
運行結果
1
|
['link5.html']
|
(7)獲取倒數第二個元素的內容
1
2
|
result = html.xpath('//li[last()-1]/a')
print result[0].text
|
運行結果
1
|
fourth item
|
(8)獲取 class 爲 bold 的標籤名
1
2
|
result = html.xpath('//*[@class="bold"]')
print result[0].tag
|
運行結果
1
|
span
|
經過以上實例的練習,相信你們對 XPath 的基本用法有了基本的瞭解。也能夠利用 text 方法來獲取元素的內容。
你們多加練習!
XPath 是一個很是好用的解析方法,同時也做爲爬蟲學習的基礎,在後面的 selenium 以及 scrapy 框架中都會涉及到這部分知識,但願你們能夠把它的語法掌握清楚,爲後面的深刻研究作好鋪墊。