scrapy的Selector选择器其实也可以用来解析,今天主要总结下css和xpath的用法,其实我个人最喜欢用css

以慕课网嵩天老师教程中的一个网页为例,python123.io/ws/demo.html

解析是提取信息的一种手段,主要提取的信息包括:标签节点、属性、文本,下面从这三个方面来分别说明

一、提取标签节点

response = ”<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>
</body></html>”

上面这个就是网页的html信息了,比如我要提取<p>标签

使用css选择器

selector = Selector(text=response)
p = selector.css('p').extract()
print(p)
#['<p class="title"><b>The demo python introduces several python courses.</b></p>', '<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>']

这样就得到了所有p节点的信息,得到的是一个列表信息,如果只想得到第一个,实际上可以使用extract_first()方法,而不是使用extract()方法

对于简单的节点查找,这样就够了,但是如果同样的节点很多,而且我要查找的节点不在第一个,这样处理就不行。解决的方法是添加限制条件,添加class、id等等限制信息

比如我想提取class=course的p节点信息,使用p[class='course'],当然,如果有其他的属性,也可以用其他属性作为限定

selecor = Selector(text=result)
response = selecor.css('p[class="course"]').extract_first()
print(response) #<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>

使用xpath

使用xpath大体思路也是一样的,只不过语法有点不同

使用xpath实现上述第一个例子

selecor = Selector(text=result)
response = selecor.xpath('//p').extract_first()
print(response)

使用xpath实现上述第二个例子

selecor = Selector(text=result)
response = selecor.xpath('//p[@class="course"]').extract_first()
print(response)

细心点的可能会发现xpath选取标签节点,就比css多了个//和@,//代表从当前节点进行选择,@后面接的是属性

二、提取属性

有时候我们需要提取属性值,比如src、href

response = ”<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>
</body></html>”

还是这段例子,为了方便观看,我拷过来

比如我现在要提取第一个a标签的href

使用css

直接在标签后面加上::attr(href),attr代表提取的是属性,括号内的href代表我要提取的是哪种属性

selecor = Selector(text=result)
response = selecor.css('a::attr(href)').extract_first()
print(response)
#http://www.icourse163.org/course/BIT-268001

如果要提取特性的a标签的href属性,比如第二个a标签的href,同样可以使用限制条件

selecor = Selector(text=result)
response = selecor.css('a[class="py2"]::attr(href)').extract_first()
print(response)
#http://www.icourse163.org/course/BIT-1001870001

使用xpath

实现上面第一个例子

selecor = Selector(text=result)
response = selecor.xpath('//a/@href').extract_first()
print(response)

实现上面第二个例子

selecor = Selector(text=result)
response = selecor.xpath('//a[@class="py2"]/@href').extract_first()
print(response)

三、提取文本信息

response = ”<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>
</body></html>”

提取第一个a标签的文本

使用css选择器

只需要在标签后面加上::text,至于怎么选择标签参照上面

selecor = Selector(text=result)
response = selecor.css('a::text').extract_first()
print(response)
#Basic Python

选择特定标签的文本,比如第二个a标签文本,同样是加一个限制条件就好

selecor = Selector(text=result)
response = selecor.css('a[class="py2"]::text').extract_first()
print(response)
#Advanced Python

使用xpath来实现

首先是第一个例子,使用//a选择到a节点,再/text()选择到文本信息

selecor = Selector(text=result)
response = selecor.xpath('//a/text()').extract_first()
print(response)

实现第二个例子,添加xpath限制条件的时候前面一定不要忘记加@,而且text后面要加()

selecor = Selector(text=result)
response = selecor.xpath('//a[@class="py2"]/text()').extract_first()
print(response)

最后总结下:对于提取而言,xpath多了/和@符号,即使在添加限制条件时,xpath也需要在限制的属性前加@,所以这也是我喜欢css的原因,因为我懒。

scrapy中Selector的使用的更多相关文章

  1. 18.scrapy中selector的用法

    Selector是一个独立的模块. Selector主要是与scrapy结合使用的. 开启Scrapy shell: 1.打开命令行cmd 2.scrapy shell http://doc.scra ...

  2. 论Scrapy中的数据持久化

    引入 Scrapy的数据持久化,主要包括存储到数据库.文件以及内置数据存储. 那我们今天就来讲讲如何把Scrapy中的数据存储到数据库和文件当中. 终端指令存储 保证爬虫文件的parse方法中有可迭代 ...

  3. 使用scrapy中xpath选择器的一个坑点

    情景如下: 一个网页下有一个ul,这个ur下有125个li标签,每个li标签下有我们想要的 url 字段(每个 url 是唯一的)和 price 字段,我们现在要访问每个li下的url并在生成的请求中 ...

  4. scrapy框架Selector提取数据

    从页面中提取数据的核心技术是HTTP文本解析,在python中常用的模块处理: BeautifulSoup  非常流行的解析库,API简单,但解析的速度慢. lxml 是一套使用c语言编写的xml解析 ...

  5. scrapy 中用selector来提取数据的用法

      一. 基本概念 1. Selector是一个可独立使用的模块,我们可以用Selector类来构建一个选择器对象,然后调用它的相关方法如xpaht(), css()等来提取数据,如下 from sc ...

  6. scrapy中选择器用法

    一.Selector选择器介绍 python从网页中提取数据常用以下两种方法: lxml:基于ElementTree的XML解析库(也可以解析HTML),不是python的标准库 BeautifulS ...

  7. Scrapy中get和extract_first的区别

    在scrapy中,从xpath中取得selector对象后,需要取出需要的数据. 使用get以及getall获取的是带标签的数据 比如 <p>这是一段文字</p> 如果用get ...

  8. python的scrapy框架的使用 和xpath的使用 && scrapy中request和response的函数参数 && parse()函数运行机制

    这篇博客主要是讲一下scrapy框架的使用,对于糗事百科爬取数据并未去专门处理 最后爬取的数据保存为json格式 一.先说一下pyharm怎么去看一些函数在源码中的代码实现 按着ctrl然后点击函数就 ...

  9. Scrapy中使用Django的Model访问数据库

    Scrapy中使用Django的Model进行数据库访问 当已存在Django项目的时候,直接引入Django的Model来使用比较简单 # 使用以下语句添加Django项目的目录到path impo ...

随机推荐

  1. [CF107D]Crime Management

    题目大意:有一种长度为$n(n\leqslant 10^{18})$的字符串,给定$m(m\leqslant10^3)$种限制,即字符$c$出现的次数为$cnt$,若一个字符有多种限制,则满足任意一个 ...

  2. [洛谷P1640][SCOI2010]连续攻击游戏

    题目大意:有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.每种装备最多只能使用一次,且只能使用其中一种属性.装备所使用的属性值必须从1开始连续.问最多能攻击多少次? ...

  3. [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  4. 【NOIP 模拟赛】中值滤波 打表找规律

    对于这样看起来不像什么算法也没什么知识点的题,一脸懵逼的话不是手推规律就是打表找规律......... 当然还有一些超出你能力之外的数学题...... #include <cstdio> ...

  5. [HEOI2017]分手是祝愿 期望概率dp 差分

    经分析可知:I.操作每个灯可看做一种异或状态 II.每个状态可看做是一些异或状态的异或和,而且每个异或状态只能由它本身释放或放入 III.每一种异或状态只有存在不存在两中可行状态,因此这些灯只有同时处 ...

  6. 判断当前系统当前浏览器是否安装启用 Adobe Flash Player,检查在chrome中的状态

    一.判断当前所在系统 let sUserAgent = navigator.userAgent;let isWin = (navigator.platform == "Win32" ...

  7. Win7命令mklink的使用

    C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了AppData,Chrome占了1G多的空间. 当时安装Chrome浏览器时因为不能指定安装目录,所以Chrome ...

  8. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  9. ACM模板~求逆序对的个数

    #include <map> #include <set> #include <cmath> #include <ctime> #include < ...

  10. git 的证书重新设置,以及如何让git 记住提交的用户名和密码

    1.git 的证书的重新设置的命令是: git config --system --unset credential.helper 2.保存git的用户名和密码注意这里是全局保存 git config ...