scrapy之Selectors
练习url:https://doc.scrapy.org/en/latest/_static/selectors-sample1.html
一 获取文本值
xpath
In []: response.selector.xpath('//title/text()').extract_first(default='')
Out[]: 'Example website'
css
In []: response.selector.css('title::text').extract_first(default='')
Out[]: 'Example website'
注:可以省略写成:response.xpath()
二 获取属性值
xpath
In []: response.selector.xpath('//base/@href').extract_first()
Out[]: 'http://example.com/'
css
In []: response.selector.css('base::attr(href)').extract_first()
Out[]: 'http://example.com/'
注: 可以省略写成:response.css
三 xpath,css嵌套使用
因为css,xpath返回的是 SelectorList 实例,所有可以嵌套便捷的使用。
ps:获取属性,xpath,@已经实现, 并不需要 /text()
In []: response.selector.css('img').xpath('@src').extract()
Out[]:
['image1_thumb.jpg',
'image2_thumb.jpg',
'image3_thumb.jpg',
'image4_thumb.jpg',
'image5_thumb.jpg']
四 .re()
.re()
.re_first()
ps :返回的是unicode构成的列表,所以,不能嵌套使用 .re()
In []: response.selector.css('div > p:nth-of-type(2)::text').extract()
Out[]: ['333xxx']
In []: response.selector.css('div > p:nth-of-type(2)::text').extract_first()
Out[]: '333xxx'
In []: response.selector.css('div > p:nth-of-type(2)::text').re_first('\w+')
Out[]: '333xxx'
In []: response.selector.css('div > p:nth-of-type(2)::text').re_first('[A-Za-z]+')
Out[]: 'xxx'
In []: response.selector.css('div > p:nth-of-type(2)::text').re('[A-Za-z]+')
Out[]: ['xxx']
五 关于Xpath的相对路径查找的注意
查找div标签下p标签
<html lang="zh-CN">
<head>
</head>
<body>
<p></p>
<div>
<p></p>
<p></p>
</div>
</body>
</html>
错误做法:
In []: divs = response.selector.xpath('//div')
In []: for p in divs.xpath('//p'):
...: print(p.extract())
...:
<p></p>
<p></p>
<p></p>
正确做法 1:
In []: divs = response.selector.css('div')
In []: for p in divs.xpath('.//p'):
...: print(p.extract())
...:
...:
<p></p>
<p></p>
正确做法 2:
In []: divs = response.selector.css('div')
In []: for p in divs.xpath('p'):
...: print(p.extract())
...:
...:
...:
<p></p>
<p></p>
scrapy之Selectors的更多相关文章
- python爬虫scrapy的Selectors参考文档
http://doc.scrapy.org/en/1.0/topics/selectors.html#topics-selectors-htmlcode
- Scrapy里Selectors 四种基础的方法
在Scrapy里面,Selectors 有四种基础的方法xpath():返回一系列的selectors,每一个select表示一个xpath参数表达式选择的节点css():返回一系列的selector ...
- scrapy的selectors
from scrapy import Selector >>> doc = """ ... <div> ... <ul> ...
- 【Scrapy】Selectors
Constructing selectors For convenience,response objects exposes a selector on .selector attribute,it ...
- Scrapy Selectors 选择器
0. 1.参考 <用Python写网络爬虫>——2.2 三种网页抓取方法 re / lxml / BeautifulSoup 需要注意的是,lxml在内部实现中,实际上是将CSS选择器转 ...
- Scrapy进阶知识点总结(二)——选择器Selectors
1. Selectors选择器 在抓取网页时,您需要执行的最常见任务是从HTML源提取数据.有几个库可用于实现此目的,例如: BeautifulSoup是Python程序员中非常流行的Web抓取库,它 ...
- Scrapy框架——介绍、安装、命令行创建,启动、项目目录结构介绍、Spiders文件夹详解(包括去重规则)、Selectors解析页面、Items、pipelines(自定义pipeline)、下载中间件(Downloader Middleware)、爬虫中间件、信号
一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...
- scrapy框架之Selectors选择器
Selectors(选择器) 当您抓取网页时,您需要执行的最常见任务是从HTML源中提取数据.有几个库可以实现这一点: BeautifulSoup是Python程序员中非常流行的网络抓取库,它基于HT ...
- Scrapy 爬虫 使用指南 完全教程
scrapy note command 全局命令: startproject :在 project_name 文件夹下创建一个名为 project_name 的Scrapy项目. scrapy sta ...
随机推荐
- Java中的Static修饰符
static(静态.修饰符):static修饰成员变量时:static修饰成员变量时,那么该成员变量的数据就是一个共享的数据. 静态成员变量的访问方式:方式一: 使用对象进行访问. 对象.属性名 方式 ...
- Cordova 本地项目创建方法
l 创建项目 需要在终端上输入:cordova create [目录][项目ID][APP名称] 运行:cordova create hello com.example.hello hello 将在 ...
- Maven各种常用架包配置文件,保存一份
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Angular - angularjs2 一些报错的概览(数据为json格式)
{"Unterminated string literal.": "未终止的字符串文本.","Identifier expected.": ...
- GoogleTest 之路2-Googletest 入门(Primer)
Why googletest? 为啥要用GoogleTest呢? googletest 是由测试技术Team 开发的带有google 特殊的需求和限制的测试框架. 不管你在什么平台上写C++代码,go ...
- HTML5一些特殊效果分享地址集合
页面预加载图片原生js: http://www.cnblogs.com/st-leslie/articles/5274568.html HTML5 FileReader读取本地文件: http://n ...
- 【 android】When an app is installed on the external storage
When an app is installed on the external storage: The .apk file is saved to the external storage, bu ...
- python导出开发环境
1.导出开发环境的依赖包 本地开发完后,再把代码给别人之前,需要 pip freeze > pip123.txt 2.其他环境安装依赖包 pip install -r pip123.txt 其他 ...
- python3爬取墨迹天气并发送给微信好友,附源码
需求: 1. 爬取墨迹天气的信息,包括温湿度.风速.紫外线.限号情况,生活tips等信息 2. 输入需要查询的城市,自动爬取相应信息 3. 链接微信,发送给指定好友 思路比较清晰,主要分两块,一是爬虫 ...
- Virtual Friends HDU - 3172 (并查集+秩+map)
These days, you can do all sorts of things online. For example, you can use various websites to make ...