最近在研究kafka拴测,看了一堆理論的東西劫映,想動手實踐一些東西违孝,奈何手上的數(shù)據(jù)比較少,突發(fā)奇想就打算寫個爬蟲去抓一些數(shù)據(jù)來玩泳赋,順便把深入一下爬蟲技術雌桑。
之前寫過一些小爬蟲,一般就是用python的requests+lxml來爬取數(shù)據(jù)祖今。這次打算學一下python的scrapy框架來爬取數(shù)據(jù)校坑。解析網(wǎng)頁內容還是打算用lxml,lxml使用了xpath語法千诬,由于太久沒用都忘光了耍目。所以打算重新學習一下xpath語法,并做個總結徐绑,方便以后忘了可以馬上回顧邪驮。
一、xpath介紹
XPath 是一門在 XML 文檔中查找信息的語言傲茄。XPath 用于在 XML 文檔中通過元素和屬性進行導航毅访。
- XPath 使用路徑表達式在 XML 文檔中進行導航
- XPath 包含一個標準函數(shù)庫
- XPath 是 XSLT 中的主要元素
- XPath 是一個 W3C 標準
節(jié)點
在 XPath 中,有七種類型的節(jié)點:元素盘榨、屬性喻粹、文本、命名空間草巡、處理指令守呜、注釋以及文檔(根)節(jié)點。XML 文檔是被作為節(jié)點樹來對待的山憨。
二查乒、xpath語法
表達式 | 描述 |
---|---|
nodename | 選取此節(jié)點的所有子節(jié)點。 |
/ | 從根節(jié)點選取萍歉。 |
// | 從匹配選擇的當前節(jié)點選擇文檔中的節(jié)點侣颂,而不考慮它們的位置。 |
. | 選取當前節(jié)點枪孩。 |
.. | 選取當前節(jié)點的父節(jié)點憔晒。 |
@ | 選取屬性藻肄。 |
例子
以下面這個xml為例子
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
- xml.xpath("bookstore") 表示選取 bookstore 元素的所有子節(jié)點
- xml.xpath("/bookstore") 表示選取根元素 bookstore。
- xml.xpath("bookstore/book") 選取屬于 bookstore 的子元素的所有 book 元素拒担。
- xml.xpath("http://book") 選取所有 book 子元素嘹屯,而不管它們在文檔中的位置。
- xml.xpath("bookstore//book") 選擇屬于 bookstore 元素的后代的所有 book 元素从撼,而不管它們位于 bookstore 之下的什么位置州弟。
- xml.xpath("http://@lang") 選取名為 lang 的所有屬性。
謂語
路徑表達式 | 結果 |
---|---|
/bookstore/book[1] | 選取屬于 bookstore 子元素的第一個 book 元素低零。 |
/bookstore/book[last()] | 選取屬于 bookstore 子元素的最后一個 book 元素婆翔。 |
/bookstore/book[last()-1] | 選取屬于 bookstore 子元素的倒數(shù)第二個 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瘟则。 |
選取未知節(jié)點
通配符 | 描述 |
---|---|
* | 匹配任何元素節(jié)點。 |
@* | 匹配任何屬性節(jié)點枝秤。 |
node() | 匹配任何類型的節(jié)點醋拧。 |
例子:
路徑表達式 | 結果 |
---|---|
/bookstore/* | 選取 bookstore 元素的所有子元素。 |
//* | 選取文檔中的所有元素淀弹。 |
//title[@*] | 選取所有帶有屬性的 title 元素趁仙。 |
選取若干路徑
通過在路徑表達式中使用“|”運算符,您可以選取若干個路徑垦页。
-
//book/title | //book/price
選取 book 元素的所有 title 和 price 元素。 -
//title | //price
選取文檔中的所有 title 和 price 元素干奢。 -
/bookstore/book/title | //price
選取屬于 bookstore 元素的 book 元素的所有 title 元素痊焊,以及文檔中所有的 price 元素。
三忿峻、軸
軸可定義相對于當前節(jié)點的節(jié)點集薄啥。
軸名稱 | 結果 |
---|---|
ancestor | 選取當前節(jié)點的所有先輩(父、祖父等)逛尚。 |
ancestor-or-self | 選取當前節(jié)點的所有先輩(父垄惧、祖父等)以及當前節(jié)點本身。 |
attribute | 選取當前節(jié)點的所有屬性绰寞。 |
child | 選取當前節(jié)點的所有子元素到逊。 |
descendant | 選取當前節(jié)點的所有后代元素(子铣口、孫等)。 |
descendant-or-self | 選取當前節(jié)點的所有后代元素(子觉壶、孫等)以及當前節(jié)點本身脑题。 |
following | 選取文檔中當前節(jié)點的結束標簽之后的所有節(jié)點。 |
namespace | 選取當前節(jié)點的所有命名空間節(jié)點铜靶。 |
parent | 選取當前節(jié)點的父節(jié)點叔遂。 |
preceding | 選取文檔中當前節(jié)點的開始標簽之前的所有節(jié)點。 |
preceding-sibling | 選取當前節(jié)點之前的所有同級節(jié)點争剿。 |
self | 選取當前節(jié)點已艰。 |
步的語法:
軸名稱::節(jié)點測試[謂語]
例子:
例子 | 結果 |
---|---|
child::book | 選取所有屬于當前節(jié)點的子元素的 book 節(jié)點。 |
attribute::lang | 選取當前節(jié)點的 lang 屬性蚕苇。 |
child::* | 選取當前節(jié)點的所有子元素哩掺。 |
attribute::* | 選取當前節(jié)點的所有屬性。 |
child::text() | 選取當前節(jié)點的所有文本子節(jié)點捆蜀。 |
child::node() | 選取當前節(jié)點的所有子節(jié)點疮丛。 |
descendant::book | 選取當前節(jié)點的所有 book 后代。 |
ancestor::book | 選擇當前節(jié)點的所有 book 先輩辆它。 |
ancestor-or-self::book | 選取當前節(jié)點的所有 book 先輩以及當前節(jié)點(如果此節(jié)點是 book 節(jié)點) |
child::*/child::price | 選取當前節(jié)點的所有 price 孫節(jié)點誊薄。 |
四、一些函數(shù)
1. starts-with函數(shù)
獲取以xxx開頭的元素
例子:xpath('//div[stars-with(@class,"test")]')
2 contains函數(shù)
獲取包含xxx的元素
例子:xpath('//div[contains(@id,"test")]')
3 and
與的關系
例子:xpath('//div[contains(@id,"test") and contains(@id,"title")]')
4 text()函數(shù)
例子1:xpath('//div[contains(text(),"test")]')
例子2:xpath('//div[@id=""test]/text()')
五锰茉、一個lxml的xpath示例
下面這個代碼來自http://www.cnblogs.com/descusr/archive/2012/06/20/2557075.html
#coding=utf-8
from lxml import etree
html = '''
<html>
<head>
<meta name="content-type" content="text/html; charset=utf-8" />
<title>友情鏈接查詢 - 站長工具</title>
<!-- uRj0Ak8VLEPhjWhg3m9z4EjXJwc -->
<meta name="Keywords" content="友情鏈接查詢" />
<meta name="Description" content="友情鏈接查詢" />
</head>
<body>
<h1 class="heading">Top News</h1>
<p style="font-size: 200%">World News only on this page</p>
Ah, and here's some more text, by the way.
<p>... and this is a parsed fragment ...</p>
<a rel="nofollow" target="_blank">青少年發(fā)展基金會</a>
<a target="_blank">洛克王國</a>
<a target="_blank">奧拉星</a>
<a target="_blank">手機游戲</a>
<a target="_blank">手機壁紙</a>
<a target="_blank">4399小游戲</a>
<a target="_blank">91wan游戲</a>
</body>
</html>
'''
page = etree.HTML(html.lower().decode('utf-8'))
hrefs = page.xpath(u"http://a")
for href in hrefs:
print href.attrib
打印出的結果為:
{'href': 'http://www.cydf.org.cn/', 'target': '_blank', 'rel': 'nofollow'}
{'href': 'http://www.4399.com/flash/32979.htm', 'target': '_blank'}
{'href': 'http://www.4399.com/flash/35538.htm', 'target': '_blank'}
{'href': 'http://game.3533.com/game/', 'target': '_blank'}
{'href': 'http://game.3533.com/tupian/', 'target': '_blank'}
{'href': 'http://www.4399.com/', 'target': '_blank'}
{'href': 'http://www.91wan.com/', 'target': '_blank'}
如果要獲取標簽a之間的內容呢蔫,就可以用print href.text
輸出
六、總結
上面的內容大多都是抄自網(wǎng)上的一些資料飒筑。這里只是做了一個大概的總結片吊,后面如果有漏的還會補充。