scrapy框架中选择器的用法

Scrapy提取数据有自己的一套机制,被称作选择器(selectors),通过特定的Xpath或者CSS表达式来选择HTML文件的某个部分
Xpath是专门在XML文件中选择节点的语言,也可以用在HTML上。
CSS是一门将HTML文档样式化语言,选择器由它定义,并与特定的HTML元素的样式相关联。

XPath选择器

常用的路径表达式,这里列举了一些常用的,XPath的功能非常强大,内含超过100个的内建函数。
下面为常用的方法

nodeName    选取此节点的所有节点
/ 从根节点选取
// 从匹配选择的当前节点选择文档中的节点,不考虑它们的位置
. 选择当前节点
.. 选取当前节点的父节点
@ 选取属性
* 匹配任何元素节点
@* 匹配任何属性节点
Node() 匹配任何类型的节点

CSS选择器

CSS层叠样式表,语法由两个主要部分组成:选择器,一条或多条声明
Selector {declaration1;declaration2;……}

下面为常用的使用方法

.class              .color              选择class=”color”的所有元素
#id #info 选择id=”info”的所有元素
* * 选择所有元素
element p 选择所有的p元素
element,element div,p 选择所有div元素和所有p元素
element element div p 选择div标签内部的所有p元素
[attribute] [target] 选择带有targe属性的所有元素
[arrtibute=value] [target=_blank] 选择target=”_blank”的所有元素

选择器的使用例子

上面我们列举了两种选择器的常用方法

下面通过scrapy帮助文档提供的一个地址来做演示
地址:http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
这个地址的网页源码为:

    <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>

我们通过scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html来演示两种选择器的功能

获取title

这里的extract_first()就可以获取title标签的文本内容,因为我们第一个通过xpath返回的结果是一个列表,所以我们通过extract()之后返回的也是一个列表,而extract_first()可以直接返回第一个值,extract_first()有一个参数default,例如:extract_first(default="")表示如果匹配不到返回一个空

In [1]: response.xpath('//title/text()')
Out[1]: [<Selector xpath='//title/text()' data='Example website'>] In [2]: response.xpath('//title/text()').extract_first()
Out[2]: 'Example website' In [6]: response.xpath('//title/text()').extract()
Out[6]: ['Example website']

同样的我们也可以通过css选择器获取,例子如下:

In [7]: response.css('title::text')
Out[7]: [<Selector xpath='descendant-or-self::title/text()' data='Example website'>] In [8]: response.css('title::text').extract_first()
Out[8]: 'Example website'

查找图片信息
这里通过xpath和css结合使用获取图片的src地址:

In [13]: response.xpath('//div[@id="images"]').css('img')
Out[13]:
[<Selector xpath='descendant-or-self::img' data='<img src="data:image1_thumb.jpg">'>,
<Selector xpath='descendant-or-self::img' data='<img src="data:image2_thumb.jpg">'>,
<Selector xpath='descendant-or-self::img' data='<img src="data:image3_thumb.jpg">'>,
<Selector xpath='descendant-or-self::img' data='<img src="data:image4_thumb.jpg">'>,
<Selector xpath='descendant-or-self::img' data='<img src="data:image5_thumb.jpg">'>] In [14]: response.xpath('//div[@id="images"]').css('img::attr(src)').extract()
Out[14]:
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg']

查找a标签信息
这里分别通过xapth和css选择器获取a标签的href内容,以及文本信息,css获取属性信息是通过attr,xpath是通过@属性名

In [15]: response.xpath('//a/@href')
Out[15]:
[<Selector xpath='//a/@href' data='image1.html'>,
<Selector xpath='//a/@href' data='image2.html'>,
<Selector xpath='//a/@href' data='image3.html'>,
<Selector xpath='//a/@href' data='image4.html'>,
<Selector xpath='//a/@href' data='image5.html'>] In [16]: response.xpath('//a/@href').extract()
Out[16]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] In [17]: response.css('a::attr(href)')
Out[17]:
[<Selector xpath='descendant-or-self::a/@href' data='image1.html'>,
<Selector xpath='descendant-or-self::a/@href' data='image2.html'>,
<Selector xpath='descendant-or-self::a/@href' data='image3.html'>,
<Selector xpath='descendant-or-self::a/@href' data='image4.html'>,
<Selector xpath='descendant-or-self::a/@href' data='image5.html'>] In [18]: response.css('a::attr(href)').extract()
Out[18]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] In [27]: response.css('a::text').extract()
Out[27]:
['Name: My image 1 ',
'Name: My image 2 ',
'Name: My image 3 ',
'Name: My image 4 ',
'Name: My image 5 '] In [28]: response.xpath('//a/text()').extract()
Out[28]:
['Name: My image 1 ',
'Name: My image 2 ',
'Name: My image 3 ',
'Name: My image 4 ',
'Name: My image 5 '] In [29]:

高级用法
查找属性名称包含img的所有的超链接,通过contains实现

In [36]: response.xpath('//a[contains(@href,"image")]/@href').extract()
Out[36]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] In [37]: response.css('a[href*=image]::attr(href)').extract()
Out[37]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] In [38]:

查找img的src属性

In [41]: response.xpath('//a[contains(@href,"image")]/img/@src').extract()
Out[41]:
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg'] In [42]: response.css('a[href*=image] img::attr(src)').extract()
Out[42]:
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg'] In [43]:

提取a标签的文本中name后面的内容,这里提供了正则的方法re和re_first

In [43]: response.css('a::text').re('Name\:(.*)')
Out[43]:
[' My image 1 ',
' My image 2 ',
' My image 3 ',
' My image 4 ',
' My image 5 '] In [44]: response.css('a::text').re_first('Name\:(.*)')
Out[44]: ' My image 1 '
 

scrapy框架中选择器的用法的更多相关文章

  1. Scrapy框架中选择器的用法【转】

    Python爬虫从入门到放弃(十四)之 Scrapy框架中选择器的用法 请给作者点赞 --> 原文链接 Scrapy提取数据有自己的一套机制,被称作选择器(selectors),通过特定的Xpa ...

  2. Python爬虫从入门到放弃(十四)之 Scrapy框架中选择器的用法

    Scrapy提取数据有自己的一套机制,被称作选择器(selectors),通过特定的Xpath或者CSS表达式来选择HTML文件的某个部分Xpath是专门在XML文件中选择节点的语言,也可以用在HTM ...

  3. Python之爬虫(十六) Scrapy框架中选择器的用法

    Scrapy提取数据有自己的一套机制,被称作选择器(selectors),通过特定的Xpath或者CSS表达式来选择HTML文件的某个部分Xpath是专门在XML文件中选择节点的语言,也可以用在HTM ...

  4. scrapy框架中Download Middleware用法

    scrapy框架中Download Middleware用法   Downloader Middleware处理的过程主要在调度器发送requests请求的时候以及网页将response结果返回给sp ...

  5. scrapy框架中Item Pipeline用法

    scrapy框架中item pipeline用法 当Item 在Spider中被收集之后,就会被传递到Item Pipeline中进行处理 每个item pipeline组件是实现了简单的方法的pyt ...

  6. 4-----Scrapy框架中选择器的用法

    Scrapy提取数据有自己的一套机制,被称作选择器(selectors),通过特定的Xpath或者CSS表达式来选择HTML文件的某个部分Xpath是专门在XML文件中选择节点的语言,也可以用在HTM ...

  7. Python爬虫从入门到放弃(十六)之 Scrapy框架中Item Pipeline用法

    当Item 在Spider中被收集之后,就会被传递到Item Pipeline中进行处理 每个item pipeline组件是实现了简单的方法的python类,负责接收到item并通过它执行一些行为, ...

  8. Python之爬虫(十八) Scrapy框架中Item Pipeline用法

    当Item 在Spider中被收集之后,就会被传递到Item Pipeline中进行处理 每个item pipeline组件是实现了简单的方法的python类,负责接收到item并通过它执行一些行为, ...

  9. Python爬虫从入门到放弃(十七)之 Scrapy框架中Download Middleware用法

    这篇文章中写了常用的下载中间件的用法和例子.Downloader Middleware处理的过程主要在调度器发送requests请求的时候以及网页将response结果返回给spiders的时候,所以 ...

随机推荐

  1. MT6737 Android N 平台 Audio系统学习----录音到播放录音流程分析

    http://blog.csdn.net/u014310046/article/details/54133688 本文将从主mic录音到播放流程来进行学习mtk audio系统架构.  在AudioF ...

  2. Discuz/X3.1去掉标题中的Powered by Discuz!以及解决首页标题后的"-"

    虽然不提倡大家去掉版权信息,但是在实际操作的时候还是去掉,毕竟每个页面标题最后面出现”Powered by Discuz!“会显得页面标题比较冗长. 经过本人的实践,论坛里也有操作方法,不过那个操作方 ...

  3. HTML(DOM)与JavaScript嵌套数组之间相互转换

    html2ja:将html目标元素解析为JavaScript数组字面量,每项的值为tagName, className, id等CSS选择器组合: showJa:将html2ja生成的数组缩进格式化显 ...

  4. CodeForces768B:Code For 1 (分治)

    Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On hi ...

  5. [SHOI 2017] 组合数问题

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4870 [算法] 回顾组合数的定义 : C(N , M)表示将N个小球放入M个盒子里的 ...

  6. react之redux增加删除数字

    比如在页面中添加和删除‘222’ action.js export const ADD= 'ADD'; export const RED='RED'; export const add=(str)=& ...

  7. 激活层和pooling的作用

    激活层: 激活函数其中一个重要的作用是加入非线性因素的,将特征映射到高维的非线性区间进行解释,解决线性模型所不能解决的问题 pooling层: 1. invariance(不变性),这种不变性包括tr ...

  8. MTK HDMI 流程

    一.HDMI初始化 1. kernel-3.18/drivers/misc/mediatek/ext_disp/mtk_extd_mgr.c static int __init mtk_extd_mg ...

  9. spark运行模式之一:Spark的local模式安装部署

    Spark运行模式 Spark 有很多种模式,最简单就是单机本地模式,还有单机伪分布式模式,复杂的则运行在集群中,目前能很好的运行在 Yarn和 Mesos 中,当然 Spark 还有自带的 Stan ...

  10. NLP | 自然语言处理 - 语言模型(Language Modeling)

    转:http://blog.csdn.net/lanxu_yy/article/details/29918015 为什么需要语言模型? 想象“语音识别”这样的场景,机器通过一定的算法将语音转换为文字, ...