在你爬取网页的时候,最普遍的事情就是在页面源码中提取需要的数据,我们有几个库可以帮你完成这个任务:
而Scrapy实现了自己的数据提取机制,它们被称为选择器,通过XPath或CSS表达式在HTML文档中来选择特定的部分
XPath是一用来在XML中选择节点的语言,同时可以用在HTML上面。CSS是一种HTML文档上面的样式语言。
Scrapy选择器构建在lxml基础之上,所以可以保证速度和准确性。
本章我们来详细讲解下选择器的工作原理,还有它们极其简单和相似的API,比lxml的API少多了,因为lxml可以用于很多其他领域。
完整的API请查看Selector参考
Scrapy帮我们下载完页面后,我们怎样在满是html标签的内容中找到我们所需要的元素呢,这里就需要使用到选择器了,它们是用来定位元素并且提取元素的值。先来举几个例子看看:
节点, 它位于html文档的
节点内
节点的内容.class="mine"
的div元素Scrapy使用css和xpath选择器来定位元素,它有四个基本方法:
下面我们通过Scrapy shell演示下选择器的使用,假设我们有如下的一个网页http://doc.scrapy.org/en/latest/_static/selectors-sample1.html,内容如下:
Example website
首先我们打开shell
scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
运行
>>> response.xpath('//title/text()')
[]
>>> response.css('title::text')
[]
结果可以看出,xpath()
和css()
方法返回的是SelectorList
实例,是一个选择器列表,你可以选择嵌套的数据:
>>> 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()
才能提取最终的数据,如果你只想获得第一个匹配的,可以使用.extract_first()
>>> response.xpath('//div[@id="images"]/a/text()').extract_first()
u'Name: My image 1 '
如果没有找到,会返回None
,还可选择默认值
>>> response.xpath('//div[@id="not-exists"]/text()').extract_first(default='not-found')
'not-found'
而CSS选择器还可以使用CSS3标准:
>>> response.css('title::text').extract()
[u'Example website']
下面是几个比较全面的示例:
>>> 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']
xpath()
和css()
返回的是选择器列表,所以你可以继续使用它们的方法。举例来讲:
>>> links = response.xpath('//a[contains(@href, "image")]')
>>> links.extract()
[u'Name: My image 1
',
u'Name: My image 2
',
u'Name: My image 3
',
u'Name: My image 4
',
u'Name: My image 5
']
>>> 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']
Selector
有一个re()
方法通过正则表达式提取数据,它返回的是unicode字符串列表,你不能再去嵌套使用
>>> 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']
>>> response.xpath('//a[contains(@href, "image")]/text()').re_first(r'Name:\s*(.*)')
u'My image 1'
当你嵌套使用XPath时候,不要使用/
开头的,因为这个会相对文档根节点开始算起,需要使用相对路径
>>> divs = response.xpath('//div')
>>> for p in divs.xpath('.//p'): # extracts all inside
... print p.extract()
# 或者下面这个直接使用p也可以
>>> for p in divs.xpath('p'):
... print p.extract()
避免使用.//text()
,直接使用.
>>> sel.xpath("//a[contains(., 'Next Page')]").extract()
[u'Click here to go to the Next Page']
>> from scrapy import Selector
>>> sel = Selector(text='')
>>> sel.css('.shout').xpath('./time/@datetime').extract()
[u'2014-07-23 19:00']