目录
<html> <head> <title>My Website</title> </head> <body> <span>Scrapy Hello world</span> <div class='links'> <a href='one.html'>Link 1<img src='image1.jpg'/></a> <a href='two.html'>Link 2<img src='image2.jpg'/></a> <a href='three.html'>Link 3<img src='image3.jpg'/></a> </div> </body> </html>
构造选择器
可以通过 text 或 TextResponse 对象构造选择器类的实例。根据所提供的输入类型,选择器选择以下规则:
from scrapy.selector import Selector from scrapy.http import HtmlResponse
Selector(text=body).xpath('//span/text()').extract()
[u'Hello world!!!']
response = HtmlResponse(url='http://yiibai.com', body=body) Selector(response=response).xpath('//span/text()').extract()
它显示的结果为:
[u'Hello world!!!']
使用选择器
>>response.selector.xpath('//title/text()')
现在,您可以通过使用 .extract()方法提取文本数据,如下所示:
>>response.xpath('//title/text()').extract()
[u'My Website']
>>response.xpath('//div[@class="links"]/a/text()').extract()
Link 1 Link 2 Link 3
>>response.xpath('//div[@class="links"]/a/text()').extract_first()
Link 1
嵌套选择器
links = response.xpath('//a[contains(@href, "image")]') for index, link in enumerate(links): args = (index, link.xpath('@href').extract(), link.xpath('img/@src').extract()) print 'The link %d pointing to url %s and image %s' % args
Link 1 pointing to url [u'one.html'] and image [u'image1.jpg'] Link 2 pointing to url [u'two.html'] and image [u'image2.jpg'] Link 3 pointing to url [u'three.html'] and image [u'image3.jpg']
使用正则表达式选择器
Scrapy 允许使用 .re() 方法正则表达式来提取数据。从上面的HTML代码中可提取图像名称,如下图所示:
>>response.xpath('//a[contains(@href, "image")]/text()').re(r'Name:\s*(.*)')
[u'Link 1', u'Link 2', u'Link 3']
用相对的XPaths
当您使用XPaths,它是从 / 开始工作的,嵌套选择器和XPath都关联到文件的绝对路径,而不是选择器的相对路径。
>>mydiv = response.xpath('//div')
接下来,可以在里面提取所有 ‘P’ 元素,在XPath前缀加上一个句点 .//p ,如下图所示:
>>for p in mydiv.xpath('.//p').extract()
使用EXSLT扩展
EXSLT是一个社区它发出扩展XML文档转换为XHTML文档XSLT(可扩展样式表语言转换)。可以使用 EXSLT 扩展与 XPath 表达式来注册名称空间,如下列表中所示:
S.N. |
前缀用法
|
命名空间
|
---|---|---|
1 |
re
正则表达式
|
http://exslt.org/regular-expressions |
2 |
set
集合操作
|
http://exslt.org/sets |
关注右侧公众号,随时随地查看教程
Scrapy教程目录