【Rollo的Python之路】Scrapy Selector选择器的学习
选择器(Selectors)
当抓取网页时,你做的最常见的任务是从HTML源码中提取数据。现有的一些库可以达到这个目的:
- BeautifulSoup 是在程序员间非常流行的网页分析库,它基于HTML代码的结构来构造一个Python对象, 对不良标记的处理也非常合理,但它有一个缺点:慢。
- lxml 是一个基于 ElementTree (不是Python标准库的一部分)的python化的XML解析库(也可以解析HTML)。
Scrapy提取数据有自己的一套机制。它们被称作选择器(seletors),因为他们通过特定的 XPath 或者 CSS表达式来“选择” HTML文件中的某个部分。
Scrapy选择器构建于 lxml 库之上,这意味着它们在速度和解析准确性上非常相似
Scrapy selector是以 文字(text) 或 TextResponse
构造的 Selector
实例。 其根据输入的类型自动选择最优的分析方法(XML vs HTML):
>>> from scrapy.selector import Selector >>> from scrapy.http import HtmlResponse
用scrapy shell 来打一个网站来练习一下selectors选择器:(pycharm 的terminal可以练习)
scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html
HTML源码:
<html><head><base href='http://example.com/' /><title>Example website</title></head><body><div id='images'><a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a><a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a><a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a><a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a><a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a></div></body> </html>
1.1.1 选择title,用xpath的方法。selector选出来的是一个列表。
In [1]: response.selector.xpath('//title/text()')>>> Out[1]: [<Selector xpath='//title/text()' data='Example website'>]
Out[1]: [<Selector xpath='//title/text()' data='Example website'>]In [2]: response.selector.xpath('//title/text()').extract Out[2]: <bound method SelectorList.getall of [<Selector xpath='//title/text()' data='Example website'>]>In [3]: response.selector.xpath('//title/text()').extract_first() Out[3]: 'Example website
选择title,用css的方法。
In [4]: response.selector.css('title::text') Out[4]: [<Selector xpath='descendant-or-self::title/text()' data='Example website'>]In [5]: response.selector.css('title::text').extract Out[5]: <bound method SelectorList.getall of [<Selector xpath='descendant-or-self::title/text()' data='Example website'>]>In [6]: response.selector.css('title::text').extract_first Out[6]: <bound method SelectorList.get of [<Selector xpath='descendant-or-self::title/text()' data='Example website'>]>In [7]: response.selector.css('title::text').extract_first() Out[7]: 'Example website'
同样,scrapy为了response内置了selector这个参数,所有也可以不打个这selector,直接用response.xpath or response.css就可以提取
用xpath的方法。
In [8]: response.xpath('//title/text()') Out[8]: [<Selector xpath='//title/text()' data='Example website'>]In [9]: response.xpath('//title/text()').extract Out[9]: <bound method SelectorList.getall of [<Selector xpath='//title/text()' data='Example website'>]>In [10]: response.xpath('//title/text()').extract_first() Out[10]: 'Example website'
用css的方法。
In [11]: response.css('title::text') Out[11]: [<Selector xpath='descendant-or-self::title/text()' data='Example website'>]In [12]: response.css('title::text').extract() Out[12]: ['Example website']In [13]: response.css('title::text').extract Out[13]: <bound method SelectorList.getall of [<Selector xpath='descendant-or-self::title/text()' data='Example website'>]>
注意:extract_first()有用来值,dextract_first(default)
1.1.2 选择文本
>>> response.xpath('//base/@href').extract() [u'http://example.com/']>>> response.css('base::attr(href)').extract() [u'http://example.com/']>>> response.xpath('//a[contains(@href, "image")]/@href').extract() [u'image1.html',u'image2.html',u'image3.html',u'image4.html',u'image5.html']>>> response.css('a[href*=image]::attr(href)').extract() [u'image1.html',u'image2.html',u'image3.html',u'image4.html',u'image5.html']>>> response.xpath('//a[contains(@href, "image")]/img/@src').extract() [u'image1_thumb.jpg',u'image2_thumb.jpg',u'image3_thumb.jpg',u'image4_thumb.jpg',u'image5_thumb.jpg']>>> response.css('a[href*=image] img::attr(src)').extract() [u'image1_thumb.jpg',u'image2_thumb.jpg',u'image3_thumb.jpg',u'image4_thumb.jpg',u'image5_thumb.jpg']
1.1.3 可以使用RE来提取:
In [18]: response.css('a::text').re('Name\:(.*)') Out[18]: [' My image 1 ',' My image 2 ',' My image 3 ',' My image 4 ',' My image 5 ']In [19]: response.css('a::text').re_first('Name\:(.*)') Out[19]: ' My image 1 '
In [21]: response.xpath('//a/text()') Out[21]: [<Selector xpath='//a/text()' data='Name: My image 1 '>,<Selector xpath='//a/text()' data='Name: My image 2 '>,<Selector xpath='//a/text()' data='Name: My image 3 '>,<Selector xpath='//a/text()' data='Name: My image 4 '>,<Selector xpath='//a/text()' data='Name: My image 5 '>]In [22]: response.xpath('//a/text()').extract Out[22]: <bound method SelectorList.getall of [<Selector xpath='//a/text()' data='Name: My image 1 '>, <Selector xpath='//a/text()' data='Name: My image 2 '>, <Selector xpath='//a/text()' data='Name: My image 3 '>, <Selector xpath='//a/text()' data='Name: My image 4 '>, <Selector xpath='//a/text()' data='Name: My image 5 '>]> In [23]: response.xpath('//a/text()').extract() Out[23]: ['Name: My image 1 ','Name: My image 2 ','Name: My image 3 ','Name: My image 4 ','Name: My image 5 ']In [26]: response.xpath('//a/text()').re('Name\:(.*)') Out[26]: [' My image 1 ',' My image 2 ',' My image 3 ',' My image 4 ',' My image 5 ']In [27]: response.xpath('//a/text()').re_first('Name\:(.*)') Out[27]: ' My image 1 '
1.1.3 可以提取属性:
#xpath提取url In [28]: response.xpath('//base/@href').extract() Out[28]: ['http://example.com/']In [29]: response.xpath('//a/@href').extract() Out[29]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']#css提取url In [32]: response.css('a::attr(href)').extract_first() Out[32]: 'image1.html'In [33]: response.css('a::attr(href)').extract() Out[33]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']
#xpath属性包括image的url In [42]: response.xpath('//a[contains(@href,"image")]/@href') Out[42]: [<Selector xpath='//a[contains(@href,"image")]/@href' data='image1.html'>,<Selector xpath='//a[contains(@href,"image")]/@href' data='image2.html'>,<Selector xpath='//a[contains(@href,"image")]/@href' data='image3.html'>,<Selector xpath='//a[contains(@href,"image")]/@href' data='image4.html'>,<Selector xpath='//a[contains(@href,"image")]/@href' data='image5.html'>]In [43]: response.xpath('//a[contains(@href,"image")]/@href').extract() Out[43]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']In [44]: response.xpath('//a[contains(@href,"image")]/@href').extract_first() Out[44]: 'image1.html'#css属性包括image的url In [45]: response.css('a[href*=image]::attr(href)') Out[45]: [<Selector xpath="descendant-or-self::a[@href and contains(@href, 'image')]/@href" data='image1.html'>,<Selector xpath="descendant-or-self::a[@href and contains(@href, 'image')]/@href" data='image2.html'>,<Selector xpath="descendant-or-self::a[@href and contains(@href, 'image')]/@href" data='image3.html'>,<Selector xpath="descendant-or-self::a[@href and contains(@href, 'image')]/@href" data='image4.html'>,<Selector xpath="descendant-or-self::a[@href and contains(@href, 'image')]/@href" data='image5.html'>]In [46]: response.css('a[href*=image]::attr(href)').extract() Out[46]: ['image1.html', 'image2.html', 'image3.html', 'image4.html', 'image5.html']In [47]: response.css('a[href*=image]::attr(href)').extract_first() Out[47]: 'image1.html'
In [48]: response.xpath('//a[contains(@href,"image")]/img/@src').extract() Out[48]: ['image1_thumb.jpg','image2_thumb.jpg','image3_thumb.jpg','image4_thumb.jpg','image5_thumb.jpg']In [50]: response.css('a[href*=image] img::attr(src)').extract() Out[50]: ['image1_thumb.jpg','image2_thumb.jpg','image3_thumb.jpg','image4_thumb.jpg','image5_thumb.jpg']
css: 要取文本就用text,要属性就用::attr
xpath:text()取文本,@属性名,可以取属性
In [53]: response.css('a img::attr(src)').extract() Out[53]: ['image1_thumb.jpg','image2_thumb.jpg','image3_thumb.jpg','image4_thumb.jpg','image5_thumb.jpg']In [55]: response.xpath('//a/img/@src').extract() Out[55]: ['image1_thumb.jpg','image2_thumb.jpg','image3_thumb.jpg','image4_thumb.jpg','image5_thumb.jpg']
转载于:https://www.cnblogs.com/rollost/p/10917172.html
【Rollo的Python之路】Scrapy Selector选择器的学习相关推荐
- 【Rollo的Python之路】Python 多进程 学习笔记 multiprocessing
Python 多进程: 由于GIL的存在,python中的多线程并不是真正的多线程,如果想要充分地使用多核CUP的资源,在python里面大部分情况需要使用多进程,Python提供了非常好的多进程包m ...
- 【Rollo的Python之路】Python 同步条件 学习笔记 Event
Python 同步条件: 条件同步和条件变量同步差不多,只是少了锁 功能,因为条件同步设计于不访问共享资源的条件环境.event = threading.Event():条件环境对象 初始值为:Fal ...
- 从scratch到python轻松学下载_STEAM教育-[少儿创客] 从Scratch到Python——python turtle-电路城论坛 - 电子工程师学习交流园地...
从Scratch到Python--python turtle 一种比pygame更加简洁的实现 现在很多学校都开设了Scratch课程,学生可以利用Scratch创作丰富的作品,然而Scratch之后 ...
- 第063讲: 论一只爬虫的自我修养11:Scrapy框架之初窥门径 | 学习记录(小甲鱼零基础入门学习Python)
上一节课我们好不容易装好了 Scrapy,今天我们就来学习如何用好它,有些同学可能会有些疑惑,既然我们懂得了Python编写爬虫的技巧,那要这个所谓的爬虫框架又有什么用呢?其实啊,你懂得Python写 ...
- python scrapy菜鸟教程_scrapy学习笔记(一)快速入门
安装Scrapy Scrapy是一个高级的Python爬虫框架,它不仅包含了爬虫的特性,还可以方便的将爬虫数据保存到csv.json等文件中. 首先我们安装Scrapy. pip install sc ...
- python 爬虫实例-Python 爬虫:Scrapy 实例(二)
原标题:Python 爬虫:Scrapy 实例(二) 稍微增加点难度,做个所需项目多一点的,并将的结果以多种形式保存起来.我们就从网络天气预报开始. 首先要做的是确定网络天气数据的来源.打开百度,搜索 ...
- Python 框架 之 Scrapy 爬虫(二)
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取)所设计的, 也可以应 ...
- 用 Python 爬虫框架 Scrapy 爬取心目中的女神
From :http://www.cnblogs.com/wanghzh/p/5824181.html 本博文将带领你从入门到精通爬虫框架 Scrapy,最终具备爬取任何网页的数据的能力. 本文以校花 ...
- Python之 - 使用Scrapy建立一个网站抓取器,网站爬取Scrapy爬虫教程
Scrapy是一个用于爬行网站以及在数据挖掘.信息处理和历史档案等大量应用范围内抽取结构化数据的应用程序框架,广泛用于工业. 在本文中我们将建立一个从Hacker News爬取数据的爬虫,并将数据按我 ...
最新文章
- 网络游戏,原罪和救赎
- hdu 5419(数学期望)
- css列表格式属性,css list-style-type属性笔记
- React 实现 百度搜索框(简易)
- 使用Java和JCEKS进行AES-256加密
- Linux管理磁盘配额
- vue-router之路由属性配置说明
- Java的String理解
- 麒麟Linux关闭telnet,银河麒麟系统管理员使用手册(30页)-原创力文档
- [转]Git使用基础篇
- 思科网院-网络简介----Packet Tracer交换机使用的基本操作
- .net reflector 反编译失败 索引超出了数组界限问题处理方法
- 2022考研数据结构_1 绪论
- 计算机专业人士研究生论文,计算机专业研究生论文致谢
- html单元格溢出,excel如何超出单元格显示
- bootstrap网站模板10例精选欣赏
- linux设置的依赖关系,linux:dpkg:依赖关系问题使得 skype 的配置工作不能继续:问题解决方法...
- JavaWeb开发基础:连接数据库的demo和数据库连接工具类DB.java
- linux yum报错:Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile
- 计算机英语及教学法,计算机英语教学方法探析
热门文章
- 基于XML的IOC案例
- 服务器端会话技术Session|| Session的原理||Session的细节||session的钝化session的活化||session的特点||session与Cookie的区别
- Java 技术篇-利用ClipboardOwner实现实时监听剪切板功能实例演示
- Python 技术篇-用smtplib和email库实现邮件发送各种类型的附件实例演示
- PyQt5 技术篇-scrollArea不显示滚动条解决方法,Qt Designer不显示滚动条,滚动条的显示和隐藏
- 嵌入式Linux学习1——Linux常用指令1
- STM32单片机硬件I2C驱动程序(查询方式)
- C++中输入输出的十六进制八进制
- 多线程生成随机数组+双线程归并排序(C++实现)
- 华东交通大学计算机调剂,华东交通大学2018考研调剂信息