前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python爬虫 --- 2.2 Scrapy 选择器的介绍

Python爬虫 --- 2.2 Scrapy 选择器的介绍

作者头像
緣來
发布2018-09-18 17:52:09
5280
发布2018-09-18 17:52:09
举报
文章被收录于专栏:緣來來來

在使用Scrapy框架之前,我们必须先了解它是如何筛选数据的,

Scrapy提取数据有自己的一套机制,被称作选择器(selectors),通过特定的Xpath或者CSS表达式来选择HTML文件的某个部分 Xpath是专门在XML文件中选择节点的语言,也可以用在HTML上。 CSS是一门将HTML文档样式化语言,选择器由它定义,并与特定的HTML元素的样式相关联。而且这些选择器构造于‘lxml’之上,这就意味着Scrapy框架下的数据筛选有着很高的效率。

基本选择器:

Scrapy爬虫支持多种信息提取的方法:

  • Beautiful Soup
  • Lxml
  • re
  • XPath Selector
  • CSS Selector

下面我们来介绍Xpath选择器和CSS选择器的使用:

Xpath选择器

  1. 介绍一下XPath: XPath 是一门在xml文档中查找信息的语言,它可以在XML文档中对于原色和属性进行遍历。其内置了超过100个内建函数,这些函数用于对字符串值,数值、日期、时间进行比较遍历。总之是一门很方便的语言。 在网络爬虫中,我们只需要利用XPath来采集数据,所以只要掌握一些基本语法,就可以上手使用了。
  2. 基本使用语法,如下表:
  1. 实例介绍: 下面我们将以这个book.xml为例子来介绍: <html> <body> <bookstore> <book> <title>水浒传</title> <author>施耐庵</author> <price>58.95</price> </book> <book> <title>西游记</title> <author>吴承恩</author> <price>58.3</price> </book> <book> <title>三国演义</title> <author>罗贯中</author> <price>48.3</price> </book> <book> <title>红楼梦</title> <author>曹雪芹</author> <price>75</price> </book> </bookstore> </body> </html>
    • 先将我们需要使用的模块导入(调试环境为ipython): In [1]: from scrapy.selector import Selector In [2]: body = open('book.xml','r').read() In [3]: print(body) <html> <body> <bookstore> <book> <title>水浒传</title> <author>施耐庵</author> <price>58.95</price> </book> <book> <title>西游记</title> <author>吴承恩</author> <price>58.3</price> </book> <book> <title>三国演义</title> <author>罗贯中</author> <price>48.3</price> </book> <book> <title>红楼梦</title> <author>曹雪芹</author> <price>75</price> </book> </bookstore> </body> </html> In [4]: body Out[4]: '<html>\n\t<body>\n\t\t<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水浒传</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游记</title>\n\t\t\t\t<author>吴承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三国演义</title>\n\t\t\t\t<author>罗贯中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>红楼梦</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>\n\t</body>\n</html>' In [5]:
    • 下面我们来举几个小例子,说明一下如何通过xpath找到我们想要的数据: In [5]: print("如果我们要第一个book的内容") 如果我们要第一个book的内容 In [7]: Selector(text=body).xpath('/html/body/bookstore/book[1]').extract() Out[7]: ['<book>\n\t\t\t\t<title>水浒传</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>'] In [8]: print("如果我们要最后一个book的内容") 如果我们要最后一个book的内容 In [9]: Selector(text=body).xpath('/html/body/bookstore/book[last()]').extract() Out[9]: ['<book>\n\t\t\t\t<title>红楼梦</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>'] In [10]: print("如果我们要最后一个book的author属性的文本") 如果我们要最后一个book的author属性的文本 In [11]: Selector(text=body).xpath('/html/body/bookstore/book[last()]/author/text()').extract() Out[11]: ['曹雪芹'] In [12]: print("下面是xpath的嵌套使用") 下面是xpath的嵌套使用 In [13]: subbody=Selector(text=body).xpath('/html/body/bookstore/book[3]').extract() In [14]: Selector(text=subbody[0]).xpath('//author/text()').extract() Out[14]: ['罗贯中'] In [15]: Selector(text=subbody[0]).xpath('//book/author/text()').extract() Out[15]: ['罗贯中'] In [16]: Selector(text=subbody[0]).xpath('//book/title/text()').extract() Out[16]: ['三国演义'] 在Xpath中最常用的方法大该就是这些了......

CSS选择器

  1. 介绍一下CSS: 和Xpath选择器比起来,感觉CSS选择器容易一些,跟写.css时方法基本一样,就是在获取内容时和Xpath不同,这里需要注意一下。
  2. 基本使用语法,如下表:
  1. 实例介绍: 下面我们还是以这个book.xml为例子来介绍:
    • 上面xpath讲过如何导入模块了,下面我们来举几个小例子,说明一下如何通过css找到我们想要的数据: In [2]: print("如果我们要所有节点的内容") 如果我们所有节点的内容 In [3]: Selector(text=body).css('*').extract() Out[3]: ['<html>\n\t<body>\n\t\t<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水浒传</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游记</title>\n\t\t\t\t<author>吴承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三国演义</title>\n\t\t\t\t<author>罗贯中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>红楼梦</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>\n\t</body>\n</html>', '<body>\n\t\t<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水浒传</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游记</title>\n\t\t\t\t<author>吴承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三国演义</title>\n\t\t\t\t<author>罗贯中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>红楼梦</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>\n\t</body>', '<bookstore>\n\t\t\t<book>\n\t\t\t\t<title>水浒传</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>西游记</title>\n\t\t\t\t<author>吴承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>三国演义</title>\n\t\t\t\t<author>罗贯中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>\n\t\t\t<book>\n\t\t\t\t<title>红楼梦</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>\n\t\t</bookstore>', '<book>\n\t\t\t\t<title>水浒传</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>', '<title>水浒传</title>', '<author>施耐庵</author>', '<price>58.95</price>', '<book>\n\t\t\t\t<title>西游记</title>\n\t\t\t\t<author>吴承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>', '<title>西游记</title>', '<author>吴承恩</author>', '<price>58.3</price>', '<book>\n\t\t\t\t<title>三国演义</title>\n\t\t\t\t<author>罗贯中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>', '<title>三国演义</title>', '<author>罗贯中</author>', '<price>48.3</price>', '<book>\n\t\t\t\t<title>红楼梦</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>', '<title>红楼梦</title>', '<author>曹雪芹</author>', '<price>75</price>'] In [4]: print("如果我们要bookstore下的所有内容") 如果我们要bookstore下的所有内容 In [5]: Selector(text=body).css('bookstore book').extract() Out[5]: ['<book>\n\t\t\t\t<title>水浒传</title>\n\t\t\t\t<author>施耐庵</author>\n\t\t\t\t<price>58.95</price>\n\t\t\t</book>', '<book>\n\t\t\t\t<title>西游记</title>\n\t\t\t\t<author>吴承恩</author>\n\t\t\t\t<price>58.3</price>\n\t\t\t</book>', '<book>\n\t\t\t\t<title>三国演义</title>\n\t\t\t\t<author>罗贯中</author>\n\t\t\t\t<price>48.3</price>\n\t\t\t</book>', '<book>\n\t\t\t\t<title>红楼梦</title>\n\t\t\t\t<author>曹雪芹</author>\n\t\t\t\t<price>75</price>\n\t\t\t</book>'] 由于book.xml没有元素,只有节点,所以只能列举以上例子,大家可以看到,css选择器比起xpath选择器更为的简洁。

好了,以上就是对Scrapy 选择器的介绍以及简单的使用,后面我会慢慢介绍Scrapy框架的具体使用。。。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基本选择器:
    • Xpath选择器
      • CSS选择器
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档