Scrapy爬蟲——xpath與css選擇器詳解

有條件的請支持慕課實戰(zhàn)正版課程,本blog僅僅是歸納總結,自用。

一、xpath部分

1.1 xpath簡介

xpath簡介.png

1.2 xpath語法

  • 子元素:僅僅指節(jié)點下面一層的元素
  • 后代元素:指標簽下面任意層級的元素
  • 父元素、祖先(先輩)元素同理。
xpath語法圖

1.3 xpath謂語語法

謂語(Predicates)謂語用來查找某個特定的節(jié)點或者包含某個指定的值的節(jié)點。謂語被嵌在方括號中。

xpathwei'yu

1.4 xpath其他語法

通配符 描述
* 匹配任何元素節(jié)點。
@* 匹配任何屬性節(jié)點。
node() 匹配任何類型的節(jié)點。
xpath其他語法

二、css選擇器

css選擇器
css選擇器2
css選擇器3

三、scrapy選擇器實戰(zhàn)

這里是它的HTML源碼:

<html>
 <head>
  <base  />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html'>Name: My image 1 <br />![](image1_thumb.jpg)</a>
   <a href='image2.html'>Name: My image 2 <br />![](image2_thumb.jpg)</a>
   <a href='image3.html'>Name: My image 3 <br />![](image3_thumb.jpg)</a>
   <a href='image4.html'>Name: My image 4 <br />![](image4_thumb.jpg)</a>
   <a href='image5.html'>Name: My image 5 <br />![](image5_thumb.jpg)</a>
  </div>
 </body>
</html>

3.1 構造選擇器

首先, 我們打開shell:

scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
  • 接著,當shell載入后,您將獲得名為response
    的shell變量,其為響應的response, 并且在其 response.selector屬性上綁定了一個 selector。
  • 因為我們處理的是HTML,選擇器將自動使用HTML語法分析。
  • 那么,通過查看 HTML code 該頁面的源碼,我們構建一個XPath來選擇title標簽內的文字:
>>> response.selector.xpath('//title/text()')
[<Selector (text) xpath=//title/text()>]
  • 由于在response中使用XPath、CSS查詢十分普遍,因此,Scrapy提供了兩個實用的快捷方式: response.xpath()response.css():
>>> response.xpath('//title/text()')
[<Selector (text) xpath=//title/text()>]
>>> response.css('title::text')
[<Selector (text) xpath=//title/text()>]
  • 如你所見, .xpath().css()方法返回一個類 SelectorList 的實例, 它是一個新選擇器的列表。這個API可以用來快速的提取嵌套數(shù)據。

  • 為了提取真實的原文數(shù)據,你需要調用 .extract()方法如下:

>>> response.xpath('//title/text()').extract()
[u'Example website']
  • 如果想要提取到第一個匹配到的元素, 必須調用 .extract_first() selector:
>>> response.xpath('//div[@id="images"]/a/text()').extract_first()
u'Name: My image 1 '
  • 現(xiàn)在我們將得到根URL(base URL)和一些圖片鏈接:
>>> response.xpath('//base/@href').extract()
[u'http://example.com/']

>>> response.css('base::attr(href)').extract()
[u'http://example.com/']

>>> response.xpath('//a[contains(@href, "image")]/@href').extract()
[u'image1.html',
 u'image2.html',
 u'image3.html',
 u'image4.html',
 u'image5.html']

>>> response.css('a[href*=image]::attr(href)').extract()
[u'image1.html',
 u'image2.html',
 u'image3.html',
 u'image4.html',
 u'image5.html']

>>> response.xpath('//a[contains(@href, "image")]/img/@src').extract()
[u'image1_thumb.jpg',
 u'image2_thumb.jpg',
 u'image3_thumb.jpg',
 u'image4_thumb.jpg',
 u'image5_thumb.jpg']

>>> response.css('a[href*=image] img::attr(src)').extract()
[u'image1_thumb.jpg',
 u'image2_thumb.jpg',
 u'image3_thumb.jpg',
 u'image4_thumb.jpg',
 u'image5_thumb.jpg']

3.2選擇器嵌套

  • 選擇器方法( .xpath() or .css() )返回相同類型的選擇器列表,因此你也可以對這些選擇器調用選擇器方法。下面是一個例子:
>>> links = response.xpath('//a[contains(@href, "image")]')
>>> links.extract()
[u'<a href="image1.html">Name: My image 1 <br>![](image1_thumb.jpg)</a>',
 u'<a href="image2.html">Name: My image 2 <br>![](image2_thumb.jpg)</a>',
 u'<a href="image3.html">Name: My image 3 <br>![](image3_thumb.jpg)</a>',
 u'<a href="image4.html">Name: My image 4 <br>![](image4_thumb.jpg)</a>',
 u'<a href="image5.html">Name: My image 5 <br>![](image5_thumb.jpg)</a>']

>>> for index, link in enumerate(links):
        args = (index, link.xpath('@href').extract(), link.xpath('img/@src').extract())
        print 'Link number %d points to url %s and image %s' % args

Link number 0 points to url [u'image1.html'] and image [u'image1_thumb.jpg']
Link number 1 points to url [u'image2.html'] and image [u'image2_thumb.jpg']
Link number 2 points to url [u'image3.html'] and image [u'image3_thumb.jpg']
Link number 3 points to url [u'image4.html'] and image [u'image4_thumb.jpg']
Link number 4 points to url [u'image5.html'] and image [u'image5_thumb.jpg']

3.3 結合正則表達式使用選擇器(selectors)

  • Selector 也有一個 .re()方法,用來通過正則表達式來提取數(shù)據。然而,不同于使用 .xpath() 或者 .css() 方法, .re() 方法返回unicode字符串的列表。所以你無法構造嵌套式的 .re() 調用。

  • 下面是一個例子,從上面的 HTML code 中提取圖像名字:

>>> response.xpath('//a[contains(@href, "image")]/text()').re(r'Name:\s*(.*)')
[u'My image 1',
 u'My image 2',
 u'My image 3',
 u'My image 4',
 u'My image 5']
  • 另外還有一個糅合了 .extract_first().re() 的函數(shù) .re_first() . 使用該函數(shù)可以提取第一個匹配到的字符串:
>>> response.xpath('//a[contains(@href, "image")]/text()').re_first(r'Name:\s*(.*)')
u'My image 1'
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容