笔记-scrapy-selector
笔记-scrapy-selector
scrapy版本:1.5.0
1.总述
scrapy内置selector建立在lxml上。
2.使用
可以使用xpath和css方法来进行解析,两者都返回列表;
sel = Selector(text=body).xpath('//div[@class="ip_list"/text()]').extract()
selector中也可以使用re()方法进行正则解析,使用方法类似于re库;
3.类用常用属性
Selector objects
class scrapy.selector.
Selector
(response=None, text=None, type=None)
response is an HtmlResponse or an XmlResponse object that will be used for selecting and extracting data.
text is a unicode string or utf-8 encoded text for cases when a response isn’t available. Using text and response together is undefined behavior.
type defines the selector type, it can be "html", "xml" or None (default).
If type is None, the selector automatically chooses the best type based on response type (see below), or defaults to "html" in case it is used together with text.
If type is None and a response is passed, the selector type is inferred from the response type as follows:
"html" for HtmlResponse type
"xml" for XmlResponse type
"html" for anything else
Otherwise, if type is set, the selector type will be forced and no detection will occur.
re(regex)
Apply the given regex and return a list of unicode strings with the matches.
regex can be either a compiled regular expression or a string which will be compiled to a regular expression using re.compile(regex)
extract()
Serialize and return the matched nodes as a list of unicode strings. Percent encoded content is unquoted.
remove_namespaces
()
Remove all namespaces, allowing to traverse the document using namespace-less xpaths. See example below.
SelectorList对象
selector类对象是内建list的一个子类,可以理解为多个selector对象组合,对selectorlist对象使用xpath,css,extract,re方法可以理解为对list中每一个对象使用方法后再将返回组合为一个列表(注意:返回值并不是作为一个整体进行插入)。
笔记-scrapy-selector的更多相关文章
- 笔记-scrapy与twisted
笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...
- 如何理解scrapy Selector
1 scrapy Selector是什么 Selector对象本质上是对DOM tree的子树的抽象,这种抽象的目的是用于定位我们感兴趣的node.比如某次http response是一棵完整的DOM ...
- scrapy Selector用法及xpath语法
准备工作 html示例: <?xml version="1.0" encoding="UTF-8"?> <html <head> ...
- Object C学习笔记18-SEL,@ selector,Class,@class
本章是对上一章<<Object C学习笔记17-动态判断和选择器>>的一点补充,所以比较简单点. 一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体 ...
- Object C学习笔记18-SEL,@ selector,Class,@class--转
一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体介绍参数: - (id)performSelector:(SEL)aSelector; - (id)performSele ...
- scrapy selector选择器
这部分内容属于补充内容 1.xpath() 2.css() 3.正则表达式 # 多个值,列表 response.xpath('//a/text()').re('(.*?):\s(.*)') # 取第一 ...
- CSS 学习笔记——CSS Selector
CSS1 中定义的选择器 类型选择器 用于选择指定类型的元素(其实他就是 html 标签选择器),常见用法如下: body { /*对 body 元素定义样式*/ } body,div { /*同时选 ...
- scrapy笔记集合
细读http://scrapy-chs.readthedocs.io/zh_CN/latest/index.html 目录 Scrapy介绍 安装 基本命令 项目结构以及爬虫应用介绍 简单使用示例 选 ...
- scrapy 学习笔记
1.scrapy 配合 selenium.phantomJS 抓取动态页面, 单纯的selemium 加 Firefox浏览器就可以抓取动态页面了, 但开启窗口太耗资源,而且一般服务器的linux 没 ...
- 使用scrapy选择器selector解析获取百度结果
0x00 概述 需要成功安装scrapy,安装方法与本文无关,不在这多说. 0x01 配置settings 由于百度对于user-agent进行验证,所以需要添加. settings.py中找到DEF ...
随机推荐
- 申请MVP奖励时的小Tips
大家新年好,今天MSPrecious为大家带来一些申请MVP奖励时的小Tips. 本文分为三个部分 MVP是什么 如何申请MVP 申请MVP需要注意的事项 MVP是什么? 我想,点进来看这篇文章的 ...
- Asp.Net MVC Identity 2.2.1 使用技巧(二)
之前我们看到了新生成的项目中跟identity有关的有四个文件,这些文件是基础功能,并未开启identity的全部功能.现在我们先启用角色功能. 1.在App_Start文件夹中的IdentityCo ...
- July 28th 2017 Week 30th Friday
If equal affection cannot be, let the more loving be me. 如果没有相等的爱,那就让我爱多一点吧. There is seldom equal a ...
- npm WARN network …… request to https://cnpmjs.…… failed, reason: socket hang up
出现类似问题的原因是由于之前配置了镜像导致的 解决方案:删掉镜像,使用npm本身进行安装 删除镜像的命令: 方法1: npm config delete registry 方法2: npm confi ...
- 理解活在Iphone中的那些App (三)
App的生存环境之宏观环境 从用户需求变成一个产品形体的App,是一个曲折的过程.主要的过程大概如此,分析用户需求,从用户需求中提炼出比较重要的部分,然后结合自己的创意,将其转化成产品.投放市场,接受 ...
- JavaScript中的值和引用
JavaScript5中有6种基本数据类型:undefined.null.布尔值(Boolean).字符串(String).数值(Number).对象(Object) ES6中新引入一种原始数据类型: ...
- the longest distance of a binary tree
版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...
- Mybatis和Mysql的Json类型
Mysql5.7新增加了Json类型字段,但是目前Mybatis中并不支持 1.新建MybatisJsonTypeHandler.java import com.fasterxml.jackson.a ...
- ACM退役帖
不知不觉在ACM竞赛的道路上已经走了两年半了,不得不感慨时间真的是过得很快. 还记得大一的时候什么也不会,每天晚上翘晚自习来机房刷题,浑浑噩噩的经过大一的打铁,大二开始有计划系统的学习ACM知识点,直 ...
- 贪心——HDU-5969 最大的位或
HDU-5969:http://acm.hdu.edu.cn/showproblem.php?pid=5969 一开始也是分了类,觉得要两种情况,l 与 r 位数相同与不同的情况,仔细想一下,可以一起 ...