笔记-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 ...
随机推荐
- SQL Server系统常用存储过程
SQL Server系统存储过程也是好几百个,算了,还是写几个常用的. 1.sp_help 查询表的信息 执行存储过程: sp_help Person 显示结果如下: 妈了个B,有了这张图,你还不懂怎 ...
- Codeforces Round #441 (Div. 2)【A、B、C、D】
Codeforces Round #441 (Div. 2) codeforces 876 A. Trip For Meal(水题) 题意:R.O.E三点互连,给出任意两点间距离,你在R点,每次只能去 ...
- Codeforces-19D Point---线段树
题目链接: https://cn.vjudge.net/problem/CodeForces-19D 题目大意: n个操作,在200000*200000的平面上加删点 find 严格在坐标右上角,x最 ...
- Linux环境编程之IPC进程间通信(五):Posix消息队列1
对于管道和FIFO来说.必须应该先有读取者存在.否则先有写入者是没有意义的. 而消息队列则不同,它是一个消息链表,有足够写权限的线程可往别的队列中放置消息,有足够读权限的线程可从队列中取走消息.每一个 ...
- the longest distance of a binary tree
版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...
- tar 打包带软连接的文件
打包普通文件夹,压缩带参数z,创建tar.gz tar -cvf ./tmp/SK_Aug_camera.tar ./gap_40_5 但是文件夹里含有软连接,带参数 h tar -cvhf ./tm ...
- 从getshell到提权
从getshell到提权 一.起因 学校推出新的党建系统,之前党建系统还参与开发,后来因为一些原因没有开发,主要想看看这届工作室的学弟.学妹代码水平,把源码撸下来审计一下,工作室用git开发的,记着上 ...
- [LuoguP2158][SDOI2008]仪仗队
[LuoguP2158][SDOI2008]仪仗队(Link) 现在你有一个\(N \times N\)的矩阵,求你站在\((1,1)\)点能看到的点的总数. 很简洁的题面. 这道题看起来很难,但是稍 ...
- Selenium自动化测试之结果处理
Selenium自动化测试之结果处理 一.断言 断言相当于性能测试中的检查点,常用断言种类很多,具体可以查看断言API:判断预期结果和实际结果是否一致,断言成功,程序继续处理,失败则终止运行,示例如下 ...
- 大话Linux内核中锁机制之原子操作、自旋锁
转至:http://blog.sina.com.cn/s/blog_6d7fa49b01014q7p.html 很多人会问这样的问题,Linux内核中提供了各式各样的同步锁机制到底有何作用?追根到底其 ...