笔记-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. C/S架构的性能测试

    很多人关心LR在C/S架构上如何实施性能测试,我想根本原因在于两个方面,一是很多时候脚本无法录制,即LR无法成功调用被测的应用程序,二是测试脚本即使录制下来,可读性不强,往往不能运行通过,调试时无从下 ...

  2. scala当中的文件操作和网络请求

    1.读取文件当中每一行的数据 def main(args: Array[String]): Unit = { //注意文件的编码格式,如果编码格式不对,那么读取报错 val file: Buffere ...

  3. LVS.md

    LVS 概述 简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,官方站点.现在LVS已经是 Linux标准内核的一部分, ...

  4. Android如何自学----转自lavor从segmentfault

    如何自学Android 1. Java知识储备 本知识点不做重点讲解: 对于有基础的同学推荐看<Java编程思想>,巩固基础,查漏补全,了解并熟悉更多细节知识点. 对于没有基础的同学推荐看 ...

  5. angularjs select下拉搜索框

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. BZOJ2223/3524:[POI2014] Couriers(主席树)

    Description 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0 ...

  7. max函数

    无论是几维,都只返回一个最大的值 >>> a = [1,2,3] >>> np.max(a) 3 >>> a = [[2,1],[3,4]] &g ...

  8. 博客存档TensorFlow入门一 1.4编程练习

        import tensorflow as tf import numpy import matplotlib.pyplot as plt #from sklearn.model_selecti ...

  9. Maven搭建Spring MVC时使用jstl无效

    1 Maven引入依赖jar包:jstl.jar和standard.jar <dependency> <groupId>javax.servlet</groupId> ...

  10. PHP介绍(PHP入门1)

    BS架构和CS架构 B:Browser:浏览器 S:Server:服务器 C:Client:客户端 BS 浏览器和服务器的关系,通过浏览器来访问服务器,比如:百度.新浪... 优点:只要有浏览器就能访 ...