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

原文链接:https://www.fkomm.cn/article/2018/8/2/27.html

在使用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个内建函数,这些函数用于对字符串值,数值、日期、时间进行比较遍历。总之是一门很方便的语言。

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

    基本使用语法,如下表:

    pic1

  3. 实例介绍:

下面我们将以这个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]: ['三国演义']

CSS选择器

  1. 介绍一下CSS:

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

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

    在这里插入图片描述

  3. 实例介绍:

下面我们还是以这个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框架的具体使用。


相关文章和视频推荐

圆方圆学院汇集 Python + AI 名师,打造精品的 Python + AI 技术课程。 在各大平台都长期有优质免费公开课,欢迎报名收看。

公开课地址:https://ke.qq.com/course/362788?flowToken=1007321

加入python学习讨论群 78486745 ,获取资料,和广大群友一起学习。
圆方圆python技术讨论群

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ScrapyPython 中一个强大的开源网络爬虫框架,可用于从网站上抓取数据。它实现了异步网络爬取、分布式爬取、自动限速、数据存储等功能,而且易于扩展。Ins 爬虫是使用 Scrapy 框架爬取 Instagram 网站上的数据,如图片、视频、用户信息等。 在使用 Scrapy 进行 Ins 爬虫时,需要先分析 Instagram 网站上的页面结构,确定需要抓取的数据类型和相应的网页元素。然后,可以编写 Scrapy爬虫程序,按照页面结构和元素进行数据抓取和解析,并将数据保存到数据库或文件中。 下面是一个简单的 Scrapy Ins 爬虫的代码示例: ```python import scrapy class InsSpider(scrapy.Spider): name = "ins" start_urls = [ 'https://www.instagram.com/explore/tags/puppy/', ] def parse(self, response): for post in response.css('article'): yield { 'image_url': post.css('img::attr(src)').get(), 'caption': post.css('a > div > div:nth-child(2) > span::text').get() } next_page = response.css('a.coreSpriteRightPaginationArrow::attr(href)').get() if next_page is not None: yield response.follow(next_page, self.parse) ``` 在这个例子中,我们定义了一个 InsSpider 类,继承自 scrapy.Spider 类。我们指定了爬虫的名称为 "ins",指定了抓取的起始 URL,即标签为 "puppy" 的帖子。在 parse() 方法中,我们使用 CSS 选择器选择了每个帖子的图片 URL 和标题,并通过 yield 语句将它们输出。然后,我们使用 CSS 选择器选择下一页的链接,如果存在,则继续访问下一页。 以上就是一个简单的 Scrapy Ins 爬虫的示例。当然,实际的爬虫程序要更加复杂,需要考虑反爬虫机制、数据清洗和存储等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值