爬虫 Scrapy 学习系列之五:Selectors

前言

这是 Scrapy 系列学习文章之一,本章主要介绍 Selectors 相关的内容;

本文为作者的原创作品,转载需注明出处;

简介

Scrapy 定义了自己的提取数据的机制,该机制被称作 Selector,该 Selector 是根据 XPath 或者 CSS 标准语言进行定义的;

简而言之,XPath 是一门用来从 XML 文档中选取元素的语言,不过它也可以使用在 HTML 文档中提取元素;
CSS 是一门用来将 style 应用到 HTML 中的一门语言,它为特定的 HTML 元素关联对应 style 而定义了 selectors;

Scrapy selectors 是基于 lxml 类库而构建的;更多有关信息请参考 Selector API

使用 Selectors

构建 selectors

通过向 Selector 类的构造函数传入 text 或者是 TextResponse 对象来构造 selectors 实例;它会根据传入的类型(input type)自动的去选择最佳的解析规则(XML vs HTML);

1
2
>>> from scrapy.selector import Selector
>>> from scrapy.http import HtmlResponse

通过参数 text 来构建,

1
2
3
>>> body = '<html><body><span>good</span></body></html>'
>>> Selector(text=body).xpath('//span/text()').extract()
[u'good']

通过 response 来构建,

1
2
3
>>> response = HtmlResponse(url='http://example.com', body=body)
>>> Selector(response=response).xpath('//span/text()').extract()
[u'good']

为了使用方便,response 对象直接通过 .selector 暴露一个 selector 实例,

1
2
>>> response.selector.xpath('//span/text()').extract()
[u'good']

使用 selectors

本小节我们将通过 Scrapy shell 来模拟爬取网站 https://doc.scrapy.org/en/latest/_static/selectors-sample1.html 中的内容,如下,是相关的 HTML 代码,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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>

首先,让我们来打开该 shell,

1
scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html

然后,当 shell 的加载结束以后,你将会通过 shell 变量response来获取相应的 Response,然后通过response.selector来获取 Selector 对象;

因为我们处理的是 HTML,所以该 selector 将会自动的使用 HTML parser;

通过上述的 HTML code,我们通过构建 XPath 来从 title 标签中去选择文本:

1
2
>>> response.selector.xpath('//title/text()')
[<Selector (text) xpath=//title/text()>]

正是因为通过使用selectorxpath()css()来查询 Response 是非常常用的做法,因此通过response简化了相关的调用流程,包含了两个方法response.xpath()response.css()来分别取代response.selector.xpath()以及responose.selector.css()方法:

1
2
3
4
>>> response.xpath('//title/text()')
[<Selector (text) xpath=//title/text()>]
>>> response.css('title::text')
[<Selector (text) xpath=//title/text()>]

从上述的结果中可以非常明确的看到,返回的是一个 SelectorList 实例,该实例中包含了一组 selectors;通过调用 SelectorList 的相关接口使得我们可以迅速的获取到每一个 selector 元素的相关内容:

1
2
3
4
5
6
>>> response.css('img').xpath('@src').extract()
[u'image1_thumb.jpg',
u'image2_thumb.jpg',
u'image3_thumb.jpg',
u'image4_thumb.jpg',
u'image5_thumb.jpg']

可见,通过 extract() 方法便可以从 selector 中提取出所要的文本,再看一个例子,

1
2
>>> response.xpath('//title/text()').extract()
[u'Example website']

如果你只想去提取第一个相匹配的元素,你可以直接使用.extract_first()

1
2
>>> response.xpath('//div[@id="images"]/a/text()').extract_first()
u'Name: My image 1 '

如果没有找到对应的元素,将会返回None

1
2
>>> response.xpath('//div[@id="not-exists"]/text()').extract_first() is None
True

可以设置一个 default value 来取代None

1
2
>>> response.xpath('//div[@id="not-exists"]/text()').extract_first(default='not-found')
'not-found'

注意,CSS selectors 可以通过 CSS3 pseudo-elements 来选择文本或者是属性;

1
2
>>> response.css('title::text').extract()
[u'Example website']

现在,我们将会去获取一些基本的 URL 和一些 images links,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
>>> 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']

SelectorList

通过上面的描述,我们清楚的知道,response.selector.xpath()或者response.xpath()当然还包含css()的情况,返回的是一个包含多个 selectors 的列表对象 SelectorList,那么 SelectorList 是什么东西呢?为什么直接可以在该对象上直接调用xpath()css()方法呢?

首先,看看response.xpath()所返回的类型,

1
2
3
>>> divs = response.xpath('//div')
>>> type(divs)
<class 'scrapy.selector.unified.SelectorList'>

再次,看看SelectorList的源码,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class SelectorList(list):
"""
The :class:`SelectorList` class is a subclass of the builtin ``list``
class, which provides a few additional methods.
"""

# __getslice__ is deprecated but `list` builtin implements it only in Py2
def __getslice__(self, i, j):
o = super(SelectorList, self).__getslice__(i, j)
return self.__class__(o)

def __getitem__(self, pos):
o = super(SelectorList, self).__getitem__(pos)
return self.__class__(o) if isinstance(pos, slice) else o

def xpath(self, xpath, namespaces=None, **kwargs):
"""
Call the ``.xpath()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.

``query`` is the same argument as the one in :meth:`Selector.xpath`

``namespaces`` is an optional ``prefix: namespace-uri`` mapping (dict)
for additional prefixes to those registered with ``register_namespace(prefix, uri)``.
Contrary to ``register_namespace()``, these prefixes are not
saved for future calls.

Any additional named arguments can be used to pass values for XPath
variables in the XPath expression, e.g.::

selector.xpath('//a[href=$url]', url="http://www.example.com")
"""
return self.__class__(flatten([x.xpath(xpath, namespaces=namespaces, **kwargs) for x in self]))

def css(self, query):
"""
Call the ``.css()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.

``query`` is the same argument as the one in :meth:`Selector.css`
"""
return self.__class__(flatten([x.css(query) for x in self]))

def re(self, regex, replace_entities=True):
"""
Call the ``.re()`` method for each element in this list and return
their results flattened, as a list of unicode strings.

By default, character entity references are replaced by their
corresponding character (except for ``&amp;`` and ``&lt;``.
Passing ``replace_entities`` as ``False`` switches off these
replacements.
"""
return flatten([x.re(regex, replace_entities=replace_entities) for x in self])

def re_first(self, regex, default=None, replace_entities=True):
"""
Call the ``.re()`` method for the first element in this list and
return the result in an unicode string. If the list is empty or the
regex doesn't match anything, return the default value (``None`` if
the argument is not provided).

By default, character entity references are replaced by their
corresponding character (except for ``&amp;`` and ``&lt;``.
Passing ``replace_entities`` as ``False`` switches off these
replacements.
"""
for el in iflatten(x.re(regex, replace_entities=replace_entities) for x in self):
return el
else:
return default

def extract(self):
"""
Call the ``.extract()`` method for each element is this list and return
their results flattened, as a list of unicode strings.
"""
return [x.extract() for x in self]
getall = extract

def extract_first(self, default=None):
"""
Return the result of ``.extract()`` for the first element in this list.
If the list is empty, return the default value.
"""
for x in self:
return x.extract()
else:
return default
get = extract_first

由此可见,SelectorList 内部实现了和 Selector 相同的接口方法,唯一不同的就是,SelectorList 所对应的xpath()css()re()等方法内部的实现是遍历 SelectorList 里面的每一个 selector 对象,然后分别执行其对应的xpath()css()re()等方法;

Nesting selectors

通过.xpath()或者.css()方法返回的是一个包含相同类型的 selectos 的队列,所以,我们仍然可以对返回的 selector 执行.xpath().css()方法;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> links = response.xpath('//a[contains(@href, "image")]')
>>> links.extract()
[u'<a href="image1.html">Name: My image 1 <br><img src="image1_thumb.jpg"></a>',
u'<a href="image2.html">Name: My image 2 <br><img src="image2_thumb.jpg"></a>',
u'<a href="image3.html">Name: My image 3 <br><img src="image3_thumb.jpg"></a>',
u'<a href="image4.html">Name: My image 4 <br><img src="image4_thumb.jpg"></a>',
u'<a href="image5.html">Name: My image 5 <br><img src="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']

来解读下上面的用例,response.xpath()等价于执行response.selector.xpath(),可见执行的是 Selector 的xpath()方法,该方法返回的是一个selector列表;通过遍历该列表,得到每一个selector元素,然后可以继续针对该selector元素执行xpath()css()方法并获得一个新的包含 Selector 的列表,然后,我们可以遍历该 Selector 列表,然后得到每一个selector元素,然后可以继续….. 这就是 Scrapy 中有关 Selector 有趣的地方了;

通过正则表达式使用 selectors (.re())

Selector 有一个.re()方法,通过该方法可以使用正则表达式来提取数据;但是,不同于.xpath().css()的是,.re()返回的是一个 unicode strings 的列表;所以,你不能像 Nesting selectors 那样构建.re()的嵌入式调用方式;

下面的这个例子将会演示如何从上面的 HTML 代码片段中获取 image names

1
2
3
4
5
6
>>> 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']

使用相对的 XPaths

记住,如果你在使用 nesting selector 操作的同时使用了一个起始符为/的 XPath,该 XPath 将会是绝对路径而不再与当前的Selector具有相对性质;

举个例子,假定你想从一个<div>的元素中去提取所有<p>元素,首先,你会去提取所有的<div>元素:

1
>>> divs = response.xpath('//div')

一开始,你或许试图使用如下的方式,但是,这种使用方式是错误的,它的确获取到了<p>元素,但是它将会返回所有的<p>元素而不仅仅是<div>中的<p>

1
2
>>> for p in divs.xpath('//p'):  # this is wrong - gets all <p> from the whole document
... print p.extract()

正确的方式是,使用.//p作为xpath()中的参数,这样,它将会获取所有<div>元素内部的<p>元素

1
2
>>> for p in divs.xpath('.//p'):  # extracts all <p> inside
... print p.extract()

还有一种非常常用的方式,就是获取<div>的所有直接子元素<p>

1
2
>>> for p in divs.xpath('p'):
... print p.extract()

XPath 表达式中的参数

XPath 允许你引用 XPath 表达式中的参数,使用$somevariable;下面我们来通过一个例子看看是如何通过表达式参数来为”id”属性赋值的,而无需 hard code;

1
2
3
>>> # `$val` used in the expression, a `val` argument needs to be passed
>>> response.xpath('//div[@id=$val]/a/text()', val='images').extract_first()
u'Name: My image 1 '

另外的一个例子,找到这样的一个<div>元素,包含 5 个<a>子元素且其属性值为”id”的例子,

1
2
>>> response.xpath('//div[count(a)=$cnt]/@id', cnt=5).extract_first()
u'images'

parsel,该类库壮大了 Scrapy selectors,更多的相关例子可以参考 XPath variables

使用 EXSLT 扩展

Scrapy 支持 EXSLT 以及一些相关的 namespaces,

prefix namespace usage
re http://exslt.org/regular-expressions regular expressions
set http://exslt.org/sets set manipulation

正则表达式 regular expressions

当 XPath 的starts-with()或者contains()方法的功能不能满足需要的时候,test()方法就派上用场了;看下面这个例子,

通过匹配条件,后缀为数字的 “class” 属性,来获取所有相关的 links

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> from scrapy import Selector
>>> doc = """
... <div>
... <ul>
... <li class="item-0"><a href="link1.html">first item</a></li>
... <li class="item-1"><a href="link2.html">second item</a></li>
... <li class="item-inactive"><a href="link3.html">third item</a></li>
... <li class="item-1"><a href="link4.html">fourth item</a></li>
... <li class="item-0"><a href="link5.html">fifth item</a></li>
... </ul>
... </div>
... """
>>> sel = Selector(text=doc, type="html")
>>> sel.xpath('//li//@href').extract()
[u'link1.html', u'link2.html', u'link3.html', u'link4.html', u'link5.html']
>>> sel.xpath('//li[re:test(@class, "item-\d$")]//@href').extract()
[u'link1.html', u'link2.html', u'link4.html', u'link5.html']
>>>

可以看到,通过在//li的匹配规则后面通过test()方法增强了过滤条件,既是要求class属性值的后缀必须是数字才能被匹配;最后,上面这个例子非常好的诠释了,如果我要在本地构建一些 HTML 代码如何用于快速测试的方式,通过构建一个 doc 文本,将该文本用来构建我们所需要的 Selector 实例;

Set 操作

Set 操作可以在提取 text 元素之前非常方便的从 document tree 中排除部分元素;

下面这个例子通过对 itemscopes 和对应的 itemprops 进行分组去提取微观的数据(该部分内容是从 http://schema.org/Product 中提取)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
>>> doc = """
... <div itemscope itemtype="http://schema.org/Product">
... <span itemprop="name">Kenmore White 17" Microwave</span>
... <img src="kenmore-microwave-17in.jpg" alt='Kenmore 17" Microwave' />
... <div itemprop="aggregateRating"
... itemscope itemtype="http://schema.org/AggregateRating">
... Rated <span itemprop="ratingValue">3.5</span>/5
... based on <span itemprop="reviewCount">11</span> customer reviews
... </div>
...
... <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
... <span itemprop="price">$55.00</span>
... <link itemprop="availability" href="http://schema.org/InStock" />In stock
... </div>
...
... Product description:
... <span itemprop="description">0.7 cubic feet countertop microwave.
... Has six preset cooking categories and convenience features like
... Add-A-Minute and Child Lock.</span>
...
... Customer reviews:
...
... <div itemprop="review" itemscope itemtype="http://schema.org/Review">
... <span itemprop="name">Not a happy camper</span> -
... by <span itemprop="author">Ellie</span>,
... <meta itemprop="datePublished" content="2011-04-01">April 1, 2011
... <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
... <meta itemprop="worstRating" content = "1">
... <span itemprop="ratingValue">1</span>/
... <span itemprop="bestRating">5</span>stars
... </div>
... <span itemprop="description">The lamp burned out and now I have to replace
... it. </span>
... </div>
...
... <div itemprop="review" itemscope itemtype="http://schema.org/Review">
... <span itemprop="name">Value purchase</span> -
... by <span itemprop="author">Lucas</span>,
... <meta itemprop="datePublished" content="2011-03-25">March 25, 2011
... <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
... <meta itemprop="worstRating" content = "1"/>
... <span itemprop="ratingValue">4</span>/
... <span itemprop="bestRating">5</span>stars
... </div>
... <span itemprop="description">Great microwave for the price. It is small and
... fits in my apartment.</span>
... </div>
... ...
... </div>
... """
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> sel = Selector(text=doc, type="html")
>>> for scope in sel.xpath('//div[@itemscope]'):
... print "current scope:", scope.xpath('@itemtype').extract()
... props = scope.xpath('''
... set:difference(./descendant::*/@itemprop,
... .//*[@itemscope]/*/@itemprop)''')
... print " properties:", props.extract()
... print

current scope: [u'http://schema.org/Product']
properties: [u'name', u'aggregateRating', u'offers', u'description', u'review', u'review']

current scope: [u'http://schema.org/AggregateRating']
properties: [u'ratingValue', u'reviewCount']

current scope: [u'http://schema.org/Offer']
properties: [u'price', u'availability']

current scope: [u'http://schema.org/Review']
properties: [u'name', u'author', u'datePublished', u'reviewRating', u'description']

current scope: [u'http://schema.org/Rating']
properties: [u'worstRating', u'ratingValue', u'bestRating']

current scope: [u'http://schema.org/Review']
properties: [u'name', u'author', u'datePublished', u'reviewRating', u'description']

current scope: [u'http://schema.org/Rating']
properties: [u'worstRating', u'ratingValue', u'bestRating']
>>>

上述代码的执行逻辑是,首先通过遍历所有的itemscope元素,然后针对每一个itemscope元素,我们将会查找到所有的 itemprops,但是不包含itemprops中的子itemprops元素,ok,这个逻辑是通过下面这行核心代码所实现的,

1
2
3
...     props = scope.xpath('''
... set:difference(./descendant::*/@itemprop,
... .//*[@itemscope]/*/@itemprop)''')

一些有关 XPath 有用的提示

这里有一些关于使用 XPath 的有用提示,参考 ScrapingHub’s blog;如果你对 XPath 不熟悉,可以从这里开始 XPath tutorial

在某些条件中使用文本节点

当你需要使用 text 文本内容作为参数去使用 XPath string function的时候,避免使用.//text()的方式,而应当仅仅使用.即可;因为.//text()将会产生一个 text elements 的集合既是一个 node-set;而当一个 node-set 要转换为一个 string 的时候,转换的过程必须通过调用 string()、contains() 或者 start-with() 方法才能进行转换,并且只有第一个元素会被转换,其余的元素并不会被转换,这点尤其需要注意;

举一个例子,

1
2
>>> from scrapy import Selector
>>> sel = Selector(text='<a href="#">Click here to go to the <strong>Next Page</strong></a>')

得到一个 node-set

1
2
>>> sel.xpath('//a//text()').extract() # take a peek at the node-set
[u'Click here to go to the ', u'Next Page']

可以看到,该 node-set 由两个 text element 组成;那么当我们试图要获取其中一个 text element,

1
2
3
4
>>> sel.xpath("string(//a[1])").extract() # convert it to string
[u'Click here to go to the Next Page']
>>> sel.xpath("string(//a[2])").extract() # convert it to string
['']

可见,利用 string() 方法只能将 node-set 中的第一个元素转换成 string;试图转换第二个元素返回为空;

同理可以推导出,在a元素中使用.//text()得到的包含两个 text node 的 node-set 对象,同理将其通过 contains 方法转换为 string 以后并不会得到所有的 text node 的值,同理,只会返回第一个 text node 的值,既是 ‘Click here to go to the’,因此,下面这个查询是不会返回任何结果的,

1
2
>>> sel.xpath("//a[contains(.//text(), 'Next Page')]").extract()
[]

但是,如果我们使用.作为参数,则表示当前 node,所以,下面这个方式是生效的,

1
2
>>> sel.xpath("//a[contains(., 'Next Page')]").extract()
[u'<a href="#">Click here to go to the <strong>Next Page</strong></a>']

注意 //node[1] 和 (//node)[1] 的区别

  • //node[1]从所有匹配的父元素开始检索,并依次返回其第一个子元素;
  • (//node)[1]从当前文档的所有 nodes 中进行检索,然后只取其中第一个值;

例子,

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> from scrapy import Selector
>>> sel = Selector(text="""
....: <ul class="list">
....: <li>1</li>
....: <li>2</li>
....: <li>3</li>
....: </ul>
....: <ul class="list">
....: <li>4</li>
....: <li>5</li>
....: <li>6</li>
....: </ul>""")
>>> xp = lambda x: sel.xpath(x).extract()

返回相关父元素的所有第一个子元素,

1
2
>>> xp("//li[1]")
[u'<li>1</li>', u'<li>4</li>']

只会去当前文档中的第一个元素<li>

1
2
>>> xp("(//li)[1]")
[u'<li>1</li>']

返回<ul>中所有的第一个子元素

1
2
>>> xp("//ul/li[1]")
[u'<li>1</li>', u'<li>4</li>']

返回当前文档中第一个<ul>元素的第一个子元素<li>

1
2
>>> xp("(//ul/li)[1]")
[u'<li>1</li>']

如果是通过 class 进行查询,使用 CSS

因为一个元素可以包含多个 CSS classes,这种情况下使用 XPath 将会显得非常的麻烦,

1
*[contains(concat(' ', normalize-space(@class), ' '), ' someclass ')]

如果你使用@class='someclass',你将可能失去其它的 classes,如果你仅仅使用contains(@class, 'someclass')那么你可能会得到更多的但并不需要的元素;

不过,Scrapy selectors 允许你使用 selectors 所组成的链条 (chain),所以大多数的时候,你可以先使用 CSS 检索 class 的方式,当需要的时候,再转换到使用 XPath 的方式;

1
2
3
4
>>> from scrapy import Selector
>>> sel = Selector(text='<div class="hero shout"><time datetime="2014-07-23 19:00">Special date</time></div>')
>>> sel.css('.shout').xpath('./time/@datetime').extract()
[u'2014-07-23 19:00']

这比使用 xpath 的方式要简单许多,但是要记住的是,在 XPath 表达式中使用.表示紧随上一个匹配的元素,这里指的就是<div class="hero shout/>

内置的 Selectors 对象

Selector

1
class scrapy.selector.Selector(response=None, text=None, type=None)

Selector 是一个对 response 进行包装的实例,用来去检索与其内容所相关的部分;

相关参数解释如下,

response是一个 HtmlResponse 或者是一个 XmlResponse 对象;

text是一个 unicode string 或者是 utf-8 编码的 string,当 response 为 None 的时候生效,如果textresponse同时输入,则是一个未被定义的行为;

type定义了 selector 类型,有htmlxml以及Node(默认)三种类型

  • 如果typeNonetext不为NoneresponseNone,则默认选择html
  • 如果typeNonetextNoneresponse不为None,那么type的选择将会受到 response 类型的影响;
    • 如果 response 类型是 HtmlResponse,那么使用html
    • 如果 response 类型是 XmlResponse,那么使用xml
    • 如果 response 类型是其它类型,那么使用其它;
  • 如果type不为None,那么会强制使用用户自定义的type

xpath(query)

通过 xpath 的查询方式来查找对应的 nodes,当命中以后,将会返回一个 SelectorList 实例;

css(query)

不再赘述

extract()

通过一组 unicode strings 来返回被匹配的 nodes;

re(regex)

通过正则表达式进行匹配并返回被匹配的 unicode strings;需要注意的是,re()re_first()都会将 HTML 元素进行解码操作;

register_namespace(prefix, uri)

为当前的 Selector 注册指定的 namespace;如果不指定相关的 namespaces,你将不能爬取提取那些使用了非标准 namespaces 网页中的数据;看后面的例子,

remove_namespaces()

将删除所有的 namespaces,允许通过 namespace-less xpaths 来进行文档检索;看后面的例子;

nonzero()

如果这里有任何真实的内容被检索将返回True否则返回False

SelectorList

该部分参考 SelectorList

例子

HTML Response

在讲解这个例子之前,我们假定已经有一个 Selector 实例通过 HTMLResponse 进行实例化了

1
sel = Selector(html_response)
  1. 从 html_response 中检索所有<h1>的元素并返回一组 Selector 对象( SelectorList )

    1
    sel.xpath("//h1")
  2. 从 html_response 中提取所有的<h1>元素的文本内容,返回 unicode strings

    1
    2
    sel.xpath("//h1").extract()         # this includes the h1 tag
    sel.xpath("//h1/text()").extract() # this excludes the h1 tag

    注意,第一个是直接从<h1>元素中通过 extract() 方法提取出了其文本内容;第二个是从<h1>的 text node 中提取出文本内容;

  3. 遍历所有的<p>标签,然后打印出他们所有的属性:
    1
    2
    for node in sel.xpath("//p"):
    print node.xpath("@class").extract()

XML Response

1
sel = Selector(xml_response)
  1. 从 XML response body 中检索所有的<product>元素,返回一个 SelectorList 对象

    1
    sel.xpath("//product")
  2. Google Base XML Feed 中提取所有的 prices,注意,该用例需要注册一个 namespace;

    1
    2
    sel.register_namespace("g", "http://base.google.com/ns/1.0")
    sel.xpath("//g:price").extract()

Removing namespaces

如果想写一个写更简单更方便的 XPaths,你可以使用Selector.remove_namespaces()方法;

让我们通过 Github blog atom feed 作为例子来进行演示,

首先,打开 Scrapy shell

1
$ scrapy shell https://github.com/blog.atom

然后,可以看到,我们试图通过 xpath 来检索匹配的 <link> 元素,但并未成功,因为 Atom 的 XML namespace 使得它们的 nodes 含糊不清,既是没有使用标准的 namespaces;

1
2
>>> response.xpath("//link")
[]

但是,一旦我们通过Selector.remove_namespaces()方法将其所有的 namespaces 删除掉以后,所有的检索便可以生效了,

1
2
3
4
5
6
>>> response.selector.remove_namespaces()
>>> response.xpath("//link")
[<Selector xpath='//link' data=u'<link xmlns="http://www.w3.org/2005/Atom'>,
<Selector xpath='//link' data=u'<link xmlns="http://www.w3.org/2005/Atom'>,
...
]

你也许想知道为什么 remove namespaces 的行为不是默认被删掉,而是需要手动删掉?原因有二,

  1. 删除 namespaces 需要遍历文档中的所有元素并对其进行修改,所以,它是一个非常耗时的操作;
  2. 有些时候,使用 namespaces 是必要的,当万一有些元素的命名发生了冲突,这个时候就必须使用 namespaces,不过这个 cases 非常的罕见;