原文链接:https://www.fkomm.cn/article/2018/8/1/26.html

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

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

基本选择器:

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

  • Beautiful Soup
  • L
  • re
  • XPath Selector
  • CSS Selector

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

Xpath选择器

  1. 介绍一下XPath:

    XPath 是一门在 文档中查找信息的语言,它可以在 文档中对于原色和属性进行遍历。其内置了超过100个内建函数,这些函数用于对字符串值,数值、日期、时间进行比较遍历。总之是一门很方便的语言。

  2. 在网络爬虫中,我们只需要利用XPath来采集数据,所以只要掌握一些基本语法,就可以上手使用了。

    基本使用语法,如下表:

    \"pic1\"

  3. 实例介绍:

下面我们将以这个book. 为例子来介绍:

<html>
     <body>
         <bookstore>
             <book>
                 < >水浒传</ >
                 <author>施耐庵</author>
                 <price>58.95</price>
             </book>
             <book>
                 < >西游记</ >
                 <author>吴承恩</author>
                 <price>58.3</price>
             </book>
             <book>
                 < >三国演义</ >
                 <author>罗贯中</author>
                 <price>48.3</price>
             </book>
             <book>
                 < >红楼梦</ >
                 <author>曹雪芹</author>
                 <price>75</price>
             </book>
         </bookstore>
     </body>
 </html>
  • 先将我们需要使用的模块导入(调试环境为ipython):
In [1]: from scrapy.selector import Selector

  In [2]: body = open(\'book. \',\'r\').read()

  In [3]: print(body)
  <html>
      <body>
          <bookstore>
              <book>
                  < >水浒传</ >
                  <author>施耐庵</author>
                  <price>58.95</price>
              </book>
              <book>
                  < >西游记</ >
                  <author>吴承恩</author>
                  <price>58.3</price>
              </book>
              <book>
                  < >三国演义</ >
                  <author>罗贯中</author>
                  <price>48.3</price>
              </book>
              <book>
                  < >红楼梦</ >
                  <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< >水浒传</ >\\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< >西游记</ >\\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< >三国演义</ >\\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< >红楼梦</ >\\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< >水浒传</ >\\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< >红楼梦</ >\\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/ /text()\').extract()
  Out[16]: [\'三国演义\']

CSS选择器

  1. 介绍一下CSS:

    和Xpath选择器比起来,感觉CSS选择器容易一些,跟写.css时方法基本一样,就是在获取内容时和Xpath不同,这里需要注意一下。

  2. 基本使用语法,如下表:

    \"pic2\"

  3. 实例介绍:

下面我们还是以这个book. 为例子来介绍:

  • 上面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< >水浒传</ >\\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< >西游记</ >\\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< >三国演义</ >\\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< >红楼梦</ >\\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< >水浒传</ >\\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< >西游记</ >\\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< >三国演义</ >\\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< >红楼梦</ >\\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< >水浒传</ >\\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< >西游记</ >\\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< >三国演义</ >\\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< >红楼梦</ >\\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< >水浒传</ >\\n\\t\\t\\t\\t<author>施耐庵</author>\\n\\t\\t\\t\\t<price>58.95</price>\\n\\t\\t\\t</book>\',
  \'< >水浒传</ >\',
  \'<author>施耐庵</author>\',
  \'<price>58.95</price>\',
  \'<book>\\n\\t\\t\\t\\t< >西游记</ >\\n\\t\\t\\t\\t<author>吴承恩</author>\\n\\t\\t\\t\\t<price>58.3</price>\\n\\t\\t\\t</book>\',
  \'< >西游记</ >\',
  \'<author>吴承恩</author>\',
  \'<price>58.3</price>\',
  \'<book>\\n\\t\\t\\t\\t< >三国演义</ >\\n\\t\\t\\t\\t<author>罗贯中</author>\\n\\t\\t\\t\\t<price>48.3</price>\\n\\t\\t\\t</book>\',
  \'< >三国演义</ >\',
  \'<author>罗贯中</author>\',
  \'<price>48.3</price>\',
  \'<book>\\n\\t\\t\\t\\t< >红楼梦</ >\\n\\t\\t\\t\\t<author>曹雪芹</author>\\n\\t\\t\\t\\t<price>75</price>\\n\\t\\t\\t</book>\',
  \'< >红楼梦</ >\',
  \'<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< >水浒传</ >\\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< >西游记</ >\\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< >三国演义</ >\\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< >红楼梦</ >\\n\\t\\t\\t\\t<author>曹雪芹</author>\\n\\t\\t\\t\\t<price>75</price>\\n\\t\\t\\t</book>\']

由于book. 没有元素,只有节点,所以只能列举以上例子,大家可以看到,css选择器比起xpath选择器更为的简洁。

总结

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

收藏 打印