# 命令行输入:scrapy shell +链接,会自动请求url,得到的相应默认为response,开启命令行交互模式
scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html In [1]: response#response为默认相应
Out[1]: <200 https://doc.scrapy.org/en/latest/_static/selectors-sample1.html> In [2]: response.text#response.text相应的源代码
# 标准结构图如下:
response.text = '''
<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>
'''
# 1:使用选择器response.selector.xpath()/response.selector.css()
In [5]: response.selector.xpath('//title/text()').extract_first()
Out[5]: 'Example website' In [6]: response.selector.css('title::text').extract_first()
Out[6]: 'Example website'
# 2:使用选择器也可以简写为:response.xpath() / response.css() In [9]: response.css('title::text')
Out[9]: [<Selector xpath='descendant-or-self::title/text()' data='Example website'>] In [10]: response.xpath('//title/text()')
Out[10]: [<Selector xpath='//title/text()' data='Example website'>] # 3:以上可知使用.xpath() .css()返回仍然是一个选择器,若要提取里面的数据,可以用extract()提取全部,extract_first提取首个
In [7]: response.xpath('//title/text()').extract_first()
Out[7]: 'Example website' In [8]: response.css('title::text').extract_first()
Out[8]: 'Example website' # 4:可以循环进行选择
# 获取div标签里面,id = 'images'的元素, 然后继续查找img标签属性为src的内容,最终提取出来
# 就是说,包含关系用中括号[],从属关系用斜杠 /
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'] # extract_first还有default属性,如果查找不到对应的元素即返回default指定的值
In [16]: response.xpath("//div[@id='images']").css('img::attr(src)').extract_first(default='')
Out[16]: 'image1_thumb.jpg' # 查找a标签下,属性为href的元素,提取出来
In [18]: response.xpath('//a/@href').extract()
Out[18]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] In [19]: response.css('a::attr(href)').extract()
Out[19]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] # 5:查找标签的文本
In [20]: response.xpath('//a/text()').extract()
Out[20]:
['Name: My image 1 ',
'Name: My image 2 ',
'Name: My image 3 ',
'Name: My image 4 ',
'Name: My image 5 '] In [21]: response.css('a::text').extract()
Out[21]:
['Name: My image 1 ',
'Name: My image 2 ',
'Name: My image 3 ',
'Name: My image 4 ',
'Name: My image 5 '] # 6:选取标签的属性
In [34]: response.css('a::attr(href)').extract()
Out[34]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] In [39]: response.xpath('//a/@href').extract()
Out[39]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] # 查找属性名称为href 包含image的标签的属性 In [24]: response.xpath('//a[contains(@href,"image")]/@href').extract()
Out[24]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] In [25]: response.css('a[href*=image]::attr(href)').extract()
Out[25]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html'] # 查找a标签里面属性名为href,包含image,包含img,属性为src的属性
In [27]: response.xpath('//a[contains(@href,"image")]/img/@src').extract()
Out[27]:
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg'] In [28]: response.css('a[href*=image] img::attr(src)').extract()
Out[28]:
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg'] # 7:可配合正则表达式,re_first表示取第一个满足正则表达式的
In [30]: response.css('a::text').re('Name\:(.*)')
Out[30]:
[' My image 1 ',
' My image 2 ',
' My image 3 ',
' My image 4 ',
' My image 5 '] In [31]: response.css('a::text').re_first('Name\:(.*)')
Out[31]: ' My image 1 ' In [32]: response.css('a::text').re_first('Name\:(.*)').strip()#去除空格
Out[32]: 'My image 1'

scrapy选择器主要用法的更多相关文章

  1. Scrapy选择器的用法

    1.构造选择器: >>> response = HtmlResponse(url='http://example.com', body=body) >>> Sele ...

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

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

  3. scrapy框架中选择器的用法

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

  4. CSS中:before和:after选择器的用法

    在线演示这次给大家带来的是对话气泡效果,主要是演示了 :before / :after 和 border 的用法,赶快来围观吧. 阅读原文:CSS中:before和:after选择器的用法

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

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

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

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

  7. Scrapy中选择器的用法

    官方文档:https://doc.scrapy.org/en/latest/topics/selectors.html Using selectors Constructing selectors R ...

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

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

  9. scrapy选择器归纳

    python 爬虫: srcrapy框架xpath和css选择器语法 Xpath基本语法 一.常用的路径表达式: 表达式 描述 实例 nodename 选取nodename节点的所有子节点 //div ...

随机推荐

  1. Java对象流的使用

    为了让对象持久化(把对象存储到本地),可以使用java的对象流处理对象,把对象的内容写到本地存储的文件中,也可以从本地文件中读取出来.也就是常说的序列化和反序列化 主要用到了ObjectInputSt ...

  2. 【treeview】 基于jQuery的简单树形插件

    [treeview] 效果图: 前几天想把后台的目录结构通过树形插件的方法反映到前端来,在网上搜了半天只找到了这个treeview,虽然不是很好看,不过还是够用的..用treeview的前提是要有jq ...

  3. Android学习笔记2——shape

    Android有很多特别的xml文件,如常用的selector.style以及shape,熟练使用这些xml可以是我们的项目变得更个性化. 一.子标签(corners.gradient.padding ...

  4. 将 Shiro 作为应用的权限基础 一:shiro的整体架构

    将 Shiro 作为应用的权限基础 一:shiro的整体架构 近来在做一个重量级的项目,其中权限.日志.报表.工作量由我负责,工作量还是蛮大的,不过想那么多干嘛,做就是了. 这段时间,接触的东西挺多, ...

  5. 1077. Kuchiguse (20)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

  6. PHP 密码重置,发送邮件,随机长度字母数字密码

    <?php include ("database.php"); require_once ('email.class.php'); date_default_timezone ...

  7. 关于yaml语言

    yaml语言广泛用于书写配置文件. 主要特点如下: 1.使用缩进表示层级关系,缩进使用空格键(非Tab键) 2.缩进的空格数目不要求,只要相同层级的元素左侧对其即可 3.#之后的内容为注释 4.yam ...

  8. 四则运算程序(java基于控制台)

    四则运算题目生成程序(基于控制台) 一.题目描述: 1. 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n 10 -o Exercise.txt 将生成10个题目. 2. 使用 -r ...

  9. Leetcode 5——Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  10. css代码整理

    width:(宽度) height:(高度) border:1px solid red:(边框 :边框粗细 显示 颜色) border-radius:10deg:(边框变圆角) box-shadow: ...