在使用Scrapy框架之前,我們必須先了解它是如何篩選數(shù)據(jù)的,
Scrapy提取數(shù)據(jù)有自己的一套機(jī)制,被稱作選擇器(selectors),通過特定的Xpath或者CSS表達(dá)式來選擇HTML文件的某個(gè)部分
Xpath是專門在XML文件中選擇節(jié)點(diǎn)的語言,也可以用在HTML上。
CSS是一門將HTML文檔樣式化語言,選擇器由它定義,并與特定的HTML元素的樣式相關(guān)聯(lián)。而且這些選擇器構(gòu)造于‘lxml’之上,這就意味著Scrapy框架下的數(shù)據(jù)篩選有著很高的效率。
基本選擇器:
Scrapy爬蟲支持多種信息提取的方法:
- Beautiful Soup
- Lxml
- re
- XPath Selector
- CSS Selector
下面我們來介紹Xpath選擇器和CSS選擇器的使用:
Xpath選擇器
-
介紹一下XPath:
XPath 是一門在xml文檔中查找信息的語言,它可以在XML文檔中對于原色和屬性進(jìn)行遍歷。其內(nèi)置了超過100個(gè)內(nèi)建函數(shù),這些函數(shù)用于對字符串值,數(shù)值、日期、時(shí)間進(jìn)行比較遍歷??傊且婚T很方便的語言。
在網(wǎng)絡(luò)爬蟲中,我們只需要利用XPath來采集數(shù)據(jù),所以只要掌握一些基本語法,就可以上手使用了。
-
基本使用語法,如下表:
image
-
實(shí)例介紹:
下面我們將以這個(gè)book.xml為例子來介紹:
<html> <body> <bookstore> <book> <title>水滸傳</title> <author>施耐庵</author> <price>58.95</price> </book> <book> <title>西游記</title> <author>吳承恩</author> <price>58.3</price> </book> <book> <title>三國演義</title> <author>羅貫中</author> <price>48.3</price> </book> <book> <title>紅樓夢</title> <author>曹雪芹</author> <price>75</price> </book> </bookstore> </body> </html>-
先將我們需要使用的模塊導(dǎo)入(調(diào)試環(huán)境為ipython):
In [1]: from scrapy.selector import Selector In [2]: body = open('book.xml','r').read() In [3]: print(body) <html> <body> <bookstore> <book> <title>水滸傳</title> <author>施耐庵</author> <price>58.95</price> </book> <book> <title>西游記</title> <author>吳承恩</author> <price>58.3</price> </book> <book> <title>三國演義</title> <author>羅貫中</author> <price>48.3</price> </book> <book> <title>紅樓夢</title> <author>曹雪芹</author> <price>75</price> </book> </bookstore> </body> </html> In [4]: body Out[4]: '<html>\n\t<body>\n\t\t<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水滸傳</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游記</title>\n\t\t\t\t<author>吳承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三國演義</title>\n\t\t\t\t<author>羅貫中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>紅樓夢</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>\n\t</body>\n</html>' In [5]: -
下面我們來舉幾個(gè)小例子,說明一下如何通過xpath找到我們想要的數(shù)據(jù):
In [5]: print("如果我們要第一個(gè)book的內(nèi)容") 如果我們要第一個(gè)book的內(nèi)容 In [7]: Selector(text=body).xpath('/html/body/bookstore/book[1]').extract() Out[7]: ['<book>\n\t\t\t\t<title>水滸傳</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>'] In [8]: print("如果我們要最后一個(gè)book的內(nèi)容") 如果我們要最后一個(gè)book的內(nèi)容 In [9]: Selector(text=body).xpath('/html/body/bookstore/book[last()]').extract() Out[9]: ['<book>\n\t\t\t\t<title>紅樓夢</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>'] In [10]: print("如果我們要最后一個(gè)book的author屬性的文本") 如果我們要最后一個(gè)book的author屬性的文本 In [11]: Selector(text=body).xpath('/html/body/bookstore/book[last()]/author/text()').extract() Out[11]: ['曹雪芹'] In [12]: print("下面是xpath的嵌套使用") 下面是xpath的嵌套使用 In [13]: subbody=Selector(text=body).xpath('/html/body/bookstore/book[3]').extract() In [14]: Selector(text=subbody[0]).xpath('//author/text()').extract() Out[14]: ['羅貫中'] In [15]: Selector(text=subbody[0]).xpath('//book/author/text()').extract() Out[15]: ['羅貫中'] In [16]: Selector(text=subbody[0]).xpath('//book/title/text()').extract() Out[16]: ['三國演義']在Xpath中最常用的方法大該就是這些了......
-
CSS選擇器
-
介紹一下CSS:
和Xpath選擇器比起來,感覺CSS選擇器容易一些,跟寫.css時(shí)方法基本一樣,就是在獲取內(nèi)容時(shí)和Xpath不同,這里需要注意一下。
-
基本使用語法,如下表:
表達(dá)式 說明 * 選擇所有節(jié)點(diǎn) #container 選擇id為container的節(jié)點(diǎn) .container 選擇所有class包含container的節(jié)點(diǎn) li a 選取所有l(wèi)i 下所有a節(jié)點(diǎn) ul + p 選取ul后面的第一個(gè)p元素 div#container > ul 選取id為container的div的第一個(gè)ul子元素 ul ~p 選取與ul相鄰的所有p元素 a[title] 選取所有有title屬性的a元素 a[href="http://jobbole.com"] 選取所有href屬性為http://jobbole.com 的a元素 a[href*="jobbole"] 選取所有href屬性值中包含jobbole的a元素 a[href^="http"] 選取所有href屬性值中以http開頭的a元素 a[href$=".jpg"] 選取所有href屬性值中以.jpg結(jié)尾的a元素 input[type=radio]:checked 選擇選中的radio的元素 div:not(#container) 選取所有id為非container 的div屬性 li:nth-child(3) 選取第三個(gè)li元素 li:nth-child(2n) 選取第偶數(shù)個(gè)li元素 -
實(shí)例介紹:
下面我們還是以這個(gè)book.xml為例子來介紹:
-
上面xpath講過如何導(dǎo)入模塊了,下面我們來舉幾個(gè)小例子,說明一下如何通過css找到我們想要的數(shù)據(jù):
In [2]: print("如果我們要所有節(jié)點(diǎn)的內(nèi)容") 如果我們所有節(jié)點(diǎn)的內(nèi)容 In [3]: Selector(text=body).css('*').extract() Out[3]: ['<html>\n\t<body>\n\t\t<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水滸傳</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游記</title>\n\t\t\t\t<author>吳承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三國演義</title>\n\t\t\t\t<author>羅貫中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>紅樓夢</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>\n\t</body>\n</html>', '<body>\n\t\t<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水滸傳</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游記</title>\n\t\t\t\t<author>吳承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三國演義</title>\n\t\t\t\t<author>羅貫中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>紅樓夢</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>\n\t</body>', '<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水滸傳</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游記</title>\n\t\t\t\t<author>吳承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三國演義</title>\n\t\t\t\t<author>羅貫中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>紅樓夢</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>', '<book>\n\t\t\t\t<title>水滸傳</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>', '<title>水滸傳</title>', '<author>施耐庵</author>', '<price>58.95</price>', '<book>\n\t\t\t\t<title>西游記</title>\n\t\t\t\t<author>吳承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>', '<title>西游記</title>', '<author>吳承恩</author>', '<price>58.3</price>', '<book>\n\t\t\t\t<title>三國演義</title>\n\t\t\t\t<author>羅貫中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>', '<title>三國演義</title>', '<author>羅貫中</author>', '<price>48.3</price>', '<book>\n\t\t\t\t<title>紅樓夢</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>', '<title>紅樓夢</title>', '<author>曹雪芹</author>', '<price>75</price>'] In [4]: print("如果我們要bookstore下的所有內(nèi)容") 如果我們要bookstore下的所有內(nèi)容 In [5]: Selector(text=body).css('bookstore book').extract() Out[5]: ['<book>\n\t\t\t\t<title>水滸傳</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>', '<book>\n\t\t\t\t<title>西游記</title>\n\t\t\t\t<author>吳承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>', '<book>\n\t\t\t\t<title>三國演義</title>\n\t\t\t\t<author>羅貫中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>', '<book>\n\t\t\t\t<title>紅樓夢</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>']由于book.xml沒有元素,只有節(jié)點(diǎn),所以只能列舉以上例子,大家可以看到,css選擇器比起xpath選擇器更為的簡潔。
-
好了,以上就是對Scrapy 選擇器的介紹以及簡單的使用,后面我會(huì)慢慢介紹Scrapy框架的具體使用。。。
此文章同時(shí)同步到我的個(gè)人博客http://www.fkomm.cn/ 謝謝支持
