有條件的請(qǐng)支持慕課實(shí)戰(zhàn)正版課程,本blog僅僅是歸納總結(jié),自用。
一、xpath部分
1.1 xpath簡(jiǎn)介

xpath簡(jiǎn)介.png
1.2 xpath語(yǔ)法
- 子元素:僅僅指節(jié)點(diǎn)下面一層的元素
- 后代元素:指標(biāo)簽下面任意層級(jí)的元素
- 父元素、祖先(先輩)元素同理。

xpath語(yǔ)法圖
1.3 xpath謂語(yǔ)語(yǔ)法
謂語(yǔ)(Predicates)謂語(yǔ)用來(lái)查找某個(gè)特定的節(jié)點(diǎn)或者包含某個(gè)指定的值的節(jié)點(diǎn)。謂語(yǔ)被嵌在方括號(hào)中。

xpathwei'yu
1.4 xpath其他語(yǔ)法
| 通配符 | 描述 |
|---|---|
| * | 匹配任何元素節(jié)點(diǎn)。 |
| @* | 匹配任何屬性節(jié)點(diǎn)。 |
| node() | 匹配任何類(lèi)型的節(jié)點(diǎn)。 |

xpath其他語(yǔ)法
二、css選擇器

css選擇器

css選擇器2

css選擇器3
三、scrapy選擇器實(shí)戰(zhàn)
- Scrapy選擇器構(gòu)建于 lxml 庫(kù)之上,這意味著它們?cè)谒俣群徒馕鰷?zhǔn)確性上非常相似。
- 我們將使用 Scrapy shell
(提供交互測(cè)試)和位于Scrapy文檔服務(wù)器的一個(gè)樣例頁(yè)面,來(lái)解釋如何使用選擇器:
http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
這里是它的HTML源碼:
<html>
<head>
<base />
<title>Example website</title>
</head>
<body>
<div id='images'>
<a href='image1.html'>Name: My image 1 <br /></a>
<a href='image2.html'>Name: My image 2 <br /></a>
<a href='image3.html'>Name: My image 3 <br /></a>
<a href='image4.html'>Name: My image 4 <br /></a>
<a href='image5.html'>Name: My image 5 <br /></a>
</div>
</body>
</html>
3.1 構(gòu)造選擇器
首先, 我們打開(kāi)shell:
scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
- 接著,當(dāng)shell載入后,您將獲得名為
response
的shell變量,其為響應(yīng)的response, 并且在其response.selector屬性上綁定了一個(gè)selector。 - 因?yàn)槲覀兲幚淼氖荋TML,選擇器將自動(dòng)使用HTML語(yǔ)法分析。
- 那么,通過(guò)查看 HTML code 該頁(yè)面的源碼,我們構(gòu)建一個(gè)XPath來(lái)選擇
title標(biāo)簽內(nèi)的文字:
>>> response.selector.xpath('//title/text()')
[<Selector (text) xpath=//title/text()>]
- 由于在
response中使用XPath、CSS查詢(xún)十分普遍,因此,Scrapy提供了兩個(gè)實(shí)用的快捷方式:response.xpath()及response.css():
>>> response.xpath('//title/text()')
[<Selector (text) xpath=//title/text()>]
>>> response.css('title::text')
[<Selector (text) xpath=//title/text()>]
如你所見(jiàn),
.xpath()及.css()方法返回一個(gè)類(lèi) SelectorList 的實(shí)例, 它是一個(gè)新選擇器的列表。這個(gè)API可以用來(lái)快速的提取嵌套數(shù)據(jù)。為了提取真實(shí)的原文數(shù)據(jù),你需要調(diào)用
.extract()方法如下:
>>> response.xpath('//title/text()').extract()
[u'Example website']
- 如果想要提取到第一個(gè)匹配到的元素, 必須調(diào)用
.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())返回相同類(lèi)型的選擇器列表,因此你也可以對(duì)這些選擇器調(diào)用選擇器方法。下面是一個(gè)例子:
>>> links = response.xpath('//a[contains(@href, "image")]')
>>> links.extract()
[u'<a href="image1.html">Name: My image 1 <br></a>',
u'<a href="image2.html">Name: My image 2 <br></a>',
u'<a href="image3.html">Name: My image 3 <br></a>',
u'<a href="image4.html">Name: My image 4 <br></a>',
u'<a href="image5.html">Name: My image 5 <br></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 結(jié)合正則表達(dá)式使用選擇器(selectors)
Selector 也有一個(gè)
.re()方法,用來(lái)通過(guò)正則表達(dá)式來(lái)提取數(shù)據(jù)。然而,不同于使用.xpath()或者.css()方法,.re()方法返回unicode字符串的列表。所以你無(wú)法構(gòu)造嵌套式的.re()調(diào)用。下面是一個(gè)例子,從上面的 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']
- 另外還有一個(gè)糅合了
.extract_first()與.re()的函數(shù).re_first(). 使用該函數(shù)可以提取第一個(gè)匹配到的字符串:
>>> response.xpath('//a[contains(@href, "image")]/text()').re_first(r'Name:\s*(.*)')
u'My image 1'