笔记-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=Nonetext=Nonetype=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的更多相关文章

  1. 笔记-scrapy与twisted

    笔记-scrapy与twisted Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码. 在任何情况下,都不要写阻塞的代码.阻塞的代码包括: ...

  2. 如何理解scrapy Selector

    1 scrapy Selector是什么 Selector对象本质上是对DOM tree的子树的抽象,这种抽象的目的是用于定位我们感兴趣的node.比如某次http response是一棵完整的DOM ...

  3. scrapy Selector用法及xpath语法

    准备工作 html示例: <?xml version="1.0" encoding="UTF-8"?> <html <head> ...

  4. Object C学习笔记18-SEL,@ selector,Class,@class

    本章是对上一章<<Object C学习笔记17-动态判断和选择器>>的一点补充,所以比较简单点. 一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体 ...

  5. Object C学习笔记18-SEL,@ selector,Class,@class--转

    一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体介绍参数: - (id)performSelector:(SEL)aSelector; - (id)performSele ...

  6. scrapy selector选择器

    这部分内容属于补充内容 1.xpath() 2.css() 3.正则表达式 # 多个值,列表 response.xpath('//a/text()').re('(.*?):\s(.*)') # 取第一 ...

  7. CSS 学习笔记——CSS Selector

    CSS1 中定义的选择器 类型选择器 用于选择指定类型的元素(其实他就是 html 标签选择器),常见用法如下: body { /*对 body 元素定义样式*/ } body,div { /*同时选 ...

  8. scrapy笔记集合

    细读http://scrapy-chs.readthedocs.io/zh_CN/latest/index.html 目录 Scrapy介绍 安装 基本命令 项目结构以及爬虫应用介绍 简单使用示例 选 ...

  9. scrapy 学习笔记

    1.scrapy 配合 selenium.phantomJS 抓取动态页面, 单纯的selemium 加 Firefox浏览器就可以抓取动态页面了, 但开启窗口太耗资源,而且一般服务器的linux 没 ...

  10. 使用scrapy选择器selector解析获取百度结果

    0x00 概述 需要成功安装scrapy,安装方法与本文无关,不在这多说. 0x01 配置settings 由于百度对于user-agent进行验证,所以需要添加. settings.py中找到DEF ...

随机推荐

  1. ZT C/C++变量命名规则,个人习惯总结

    C/C++变量命名规则,个人习惯总结 (2012-10-31 13:48:10) 转载▼ 标签: c/c变量命名规则 c语言变量命名 c变量命名 规则规范 it 分类: C/VC C_C++变量命名规 ...

  2. [EffectiveC++]item22:Declare data members private

    将成员变量隐藏在函数接口的背后,可以为“所有可能的实现”提供弹性, 假设我们有一个public成员变量,而我们最终取消了它,多少代码可能会被破坏呢?那是一个不可知的大量. protected成员变量就 ...

  3. Mac远程连接服务器

    方法一:ssh 方法二:command+K进入远程桌面,这种方式类似于windwos下的远程桌面

  4. Asp.net & Aspose.cells 导出

    protected void btnExport_Click(object sender, EventArgs e) { DataTable tbBooks = (DataTable)Session[ ...

  5. Linux调整系统时间

    1.yum search ntp(搜索可安装的ntpn) 2.yum install ntp.x86_64 3.service ntpd start 4.ps -ef | grep ntpd 5.da ...

  6. [WebKit] JavaScriptCore解析--基础篇 (一)JSC与WebCore

    先看一下官方的基本介绍,短短几句就塞满了关键字. SquirrelFish,正式名称是JavaScriptCore,包括register-based(基于寄存器的虚拟机), direct-thread ...

  7. win10 系统下获取系统版本号为6.2的问题

    近期赶时髦升级了win10,用着挺爽.但是某天在测试一个bug时发现要对win10做特殊处理,于是直接调用了GetVersionEx,并取出版本号进行判断,但是发现得到的版本竟然是6.2.当时就被雷到 ...

  8. 6、Android---运用手机多媒体(待完成)

    6.1.程序运行在手机上 6.2.使用通知 通知是Android中比较由特色的一个功能 当某个应用程序需要向用户发出一些提示信息时 而该程序由不在前台的显示 就可以借助通知来实现 6.2.1.通知的基 ...

  9. SpringBoot实战(九)之Validator

    表单验证,是最为常见的,今天演示的是利用hibernate-validtor进行校验,有的时候,虽然前端方面通过jQuery或者require.js校验框架进行校验,可以减轻服务器的压力和改善用户体验 ...

  10. PAT——1036. 跟奥巴马一起编程

    美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个正 ...