1. 程式人生 > >Python爬蟲從入門到放棄(十四)之 Scrapy框架中選擇器的用法

Python爬蟲從入門到放棄(十四)之 Scrapy框架中選擇器的用法

esp 技術分享 val arr con des image 使用 自己

原文地址https://www.cnblogs.com/zhaof/p/7189860.html

Scrapy提取數據有自己的一套機制,被稱作選擇器(selectors),通過特定的Xpath或者CSS表達式來選擇HTML文件的某個部分
Xpath是專門在XML文件中選擇節點的語言,也可以用在HTML上。
CSS是一門將HTML文檔樣式化語言,選擇器由它定義,並與特定的HTML元素的樣式相關聯。

XPath選擇器

常用的路徑表達式,這裏列舉了一些常用的,XPath的功能非常強大,內含超過100個的內建函數。
下面為常用的方法

技術分享圖片
nodeName    選取此節點的所有節點
/           從根節點選取
//          從匹配選擇的當前節點選擇文檔中的節點,不考慮它們的位置
.           選擇當前節點
..          選取當前節點的父節點
@           選取屬性
*           匹配任何元素節點
@*          匹配任何屬性節點
Node()      匹配任何類型的節點
技術分享圖片

CSS選擇器

CSS層疊樣式表,語法由兩個主要部分組成:選擇器,一條或多條聲明
Selector {declaration1;declaration2;……}

下面為常用的使用方法

技術分享圖片
.class              .color              選擇class=”color”的所有元素
#id                 #info               選擇id=”info”的所有元素
*                   *                   選擇所有元素
element             p                   選擇所有的p元素
element,element     div,p               選擇所有div元素和所有p元素
element element     div p               選擇div標簽內部的所有p元素
[attribute]         [target]            選擇帶有targe屬性的所有元素
[arrtibute=value]   [target=_blank]     選擇target=”_blank”的所有元素
技術分享圖片

選擇器的使用例子

上面我們列舉了兩種選擇器的常用方法,下面通過scrapy幫助文檔提供的一個地址來做演示
地址:http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
這個地址的網頁源碼為:

技術分享圖片
    <html>
     <head>
      <base href=‘http://example.com/‘ />
      <title>Example website</title>
     </head>
     <body>
      <div id=‘images‘>
       <a href=‘image1.html‘>Name: My image 1 <br /><img src=‘image1_thumb.jpg‘ /></a>
       <a href=‘image2.html‘>Name: My image 2 <br /><img src=‘image2_thumb.jpg‘ /></a>
       <a href=‘image3.html‘>Name: My image 3 <br /><img src=‘image3_thumb.jpg‘ /></a>
       <a href=‘image4.html‘>Name: My image 4 <br /><img src=‘image4_thumb.jpg‘ /></a>
       <a href=‘image5.html‘>Name: My image 5 <br /><img src=‘image5_thumb.jpg‘ /></a>
      </div>
     </body>
    </html>
技術分享圖片

我們通過scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html來演示兩種選擇器的功能

獲取title

這裏的extract_first()就可以獲取title標簽的文本內容,因為我們第一個通過xpath返回的結果是一個列表,所以我們通過extract()之後返回的也是一個列表,而extract_first()可以直接返回第一個值,extract_first()有一個參數default,例如:extract_first(default="")表示如果匹配不到返回一個空

技術分享圖片
In [1]: response.xpath(‘//title/text()‘)
Out[1]: [<Selector xpath=‘//title/text()‘ data=‘Example website‘>]

In [2]: response.xpath(‘//title/text()‘).extract_first()
Out[2]: ‘Example website‘

In [6]: response.xpath(‘//title/text()‘).extract()
Out[6]: [‘Example website‘]
技術分享圖片

同樣的我們也可以通過css選擇器獲取,例子如下:

In [7]: response.css(‘title::text‘)
Out[7]: [<Selector xpath=‘descendant-or-self::title/text()‘ data=‘Example website‘>]

In [8]: response.css(‘title::text‘).extract_first()
Out[8]: ‘Example website‘

查找圖片信息
這裏通過xpath和css結合使用獲取圖片的src地址:

技術分享圖片
In [13]: response.xpath(‘//div[@id="images"]‘).css(‘img‘)
Out[13]: 
[<Selector xpath=‘descendant-or-self::img‘ data=‘<img src="image1_thumb.jpg">‘>,
 <Selector xpath=‘descendant-or-self::img‘ data=‘<img src="image2_thumb.jpg">‘>,
 <Selector xpath=‘descendant-or-self::img‘ data=‘<img src="image3_thumb.jpg">‘>,
 <Selector xpath=‘descendant-or-self::img‘ data=‘<img src="image4_thumb.jpg">‘>,
 <Selector xpath=‘descendant-or-self::img‘ data=‘<img src="image5_thumb.jpg">‘>]

In [14]: response.xpath(‘//div[@id="images"]‘).css(‘img::attr(src)‘).extract()
Out[14]: 
[‘image1_thumb.jpg‘,
 ‘image2_thumb.jpg‘,
 ‘image3_thumb.jpg‘,
 ‘image4_thumb.jpg‘,
 ‘image5_thumb.jpg‘]
技術分享圖片

查找a標簽信息
這裏分別通過xapth和css選擇器獲取a標簽的href內容,以及文本信息,css獲取屬性信息是通過attr,xpath是通過@屬性名

技術分享圖片
In [15]: response.xpath(‘//a/@href‘)
Out[15]: 
[<Selector xpath=‘//a/@href‘ data=‘image1.html‘>,
 <Selector xpath=‘//a/@href‘ data=‘image2.html‘>,
 <Selector xpath=‘//a/@href‘ data=‘image3.html‘>,
 <Selector xpath=‘//a/@href‘ data=‘image4.html‘>,
 <Selector xpath=‘//a/@href‘ data=‘image5.html‘>]

In [16]: response.xpath(‘//a/@href‘).extract()
Out[16]: [‘image1.html‘, ‘image2.html‘, ‘image3.html‘, ‘image4.html‘, ‘image5.html‘]

In [17]: response.css(‘a::attr(href)‘)
Out[17]: 
[<Selector xpath=‘descendant-or-self::a/@href‘ data=‘image1.html‘>,
 <Selector xpath=‘descendant-or-self::a/@href‘ data=‘image2.html‘>,
 <Selector xpath=‘descendant-or-self::a/@href‘ data=‘image3.html‘>,
 <Selector xpath=‘descendant-or-self::a/@href‘ data=‘image4.html‘>,
 <Selector xpath=‘descendant-or-self::a/@href‘ data=‘image5.html‘>]

In [18]: response.css(‘a::attr(href)‘).extract()
Out[18]: [‘image1.html‘, ‘image2.html‘, ‘image3.html‘, ‘image4.html‘, ‘image5.html‘]

In [27]: response.css(‘a::text‘).extract()
Out[27]: 
[‘Name: My image 1 ‘,
 ‘Name: My image 2 ‘,
 ‘Name: My image 3 ‘,
 ‘Name: My image 4 ‘,
 ‘Name: My image 5 ‘]

In [28]: response.xpath(‘//a/text()‘).extract()
Out[28]: 
[‘Name: My image 1 ‘,
 ‘Name: My image 2 ‘,
 ‘Name: My image 3 ‘,
 ‘Name: My image 4 ‘,
 ‘Name: My image 5 ‘]

In [29]: 
技術分享圖片

高級用法
查找屬性名稱包含img的所有的超鏈接,通過contains實現

技術分享圖片
In [36]: response.xpath(‘//a[contains(@href,"image")]/@href‘).extract()
Out[36]: [‘image1.html‘, ‘image2.html‘, ‘image3.html‘, ‘image4.html‘, ‘image5.html‘]

In [37]: response.css(‘a[href*=image]::attr(href)‘).extract()
Out[37]: [‘image1.html‘, ‘image2.html‘, ‘image3.html‘, ‘image4.html‘, ‘image5.html‘]

In [38]: 
技術分享圖片

查找img的src屬性

技術分享圖片
In [41]: response.xpath(‘//a[contains(@href,"image")]/img/@src‘).extract()
Out[41]: 
[‘image1_thumb.jpg‘,
 ‘image2_thumb.jpg‘,
 ‘image3_thumb.jpg‘,
 ‘image4_thumb.jpg‘,
 ‘image5_thumb.jpg‘]

In [42]: response.css(‘a[href*=image] img::attr(src)‘).extract()
Out[42]: 
[‘image1_thumb.jpg‘,
 ‘image2_thumb.jpg‘,
 ‘image3_thumb.jpg‘,
 ‘image4_thumb.jpg‘,
 ‘image5_thumb.jpg‘]

In [43]: 
技術分享圖片

提取a標簽的文本中name後面的內容,這裏提供了正則的方法re和re_first

技術分享圖片
In [43]: response.css(‘a::text‘).re(‘Name\:(.*)‘)
Out[43]: 
[‘ My image 1 ‘,
 ‘ My image 2 ‘,
 ‘ My image 3 ‘,
 ‘ My image 4 ‘,
 ‘ My image 5 ‘]

In [44]: response.css(‘a::text‘).re_first(‘Name\:(.*)‘)
Out[44]: ‘ My image 1 ‘
技術分享圖片

Python爬蟲從入門到放棄(十四)之 Scrapy框架中選擇器的用法