第七篇 css选择器实现字段解析
CSS选择器的作用实际和xpath的一样,都是为了定位具体的元素



举例我要爬取下面这个页面的标题

In []: title = response.css(".entry-header h1")
In []: title
Out[]: [<Selector xpath="descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' entry-header ')]/descendant-or-self::*/h1" data='<h1>谷歌用两年时间研究了 180 个团队,发现高效团队有这五个特征</h1>'>]
In []: title = response.css(".entry-header h1").extract()
In []: title
Out[]: ['<h1>谷歌用两年时间研究了 180 个团队,发现高效团队有这五个特征</h1>']
In []: ##可以使用css的::text取到内容
In []: title = response.css(".entry-header h1::text").extract()
In []: title
Out[]: ['谷歌用两年时间研究了 180 个团队,发现高效团队有这五个特征']
获取文章创建日期:
In []: date_text = response.css(".entry-meta-hide-on-mobile").extract()
In []: date_text
Out[]: ['<p class="entry-meta-hide-on-mobile">\r\n\r\n 2017/08/23 · <a href="http://blog.jobbole.com/category/career/" rel="category tag">职场</a>\r\n \r\n · <a href="#article-comment"> 7 评论 </a>\r\n \r\n\r\n \r\n · <a href="http://blog.jobbole.com/tag/google/">Google</a>, <a href="http://blog.jobbole.com/tag/%e5%9b%a2%e9%98%9f/">团队</a>\r\n \r\n</p>']
In []: date_text = response.css(".entry-meta-hide-on-mobile::text").extract()
In []: date_text
Out[]:
['\r\n\r\n 2017/08/23 · ',
'\r\n \r\n · ',
'\r\n \r\n\r\n \r\n · ',
', ',
'\r\n \r\n']
In []: date_text = response.css(".entry-meta-hide-on-mobile::text").extract()[
...: ]
In []: date_text
Out[]: '\r\n\r\n 2017/08/23 · '
In []: date_text = response.css(".entry-meta-hide-on-mobile::text").extract()[
...: ].strip()
In []: date_text
Out[]: '2017/08/23 ·'
In []: date_text = response.css(".entry-meta-hide-on-mobile::text").extract()[
...: ].strip().replace("·","").strip()
In []: date_text
Out[]: '2017/08/23'
获取评论数

In []: comment_num = response.css("a[href='#article-comment']")
In []: comment_num
Out[]:
[<Selector xpath="descendant-or-self::a[@href = '#article-comment']" data='<a href="#article-comment"> 7 评论 </a>'>,
<Selector xpath="descendant-or-self::a[@href = '#article-comment']" data='<a href="#article-comment"><span class="'>]
In []: comment_num = response.css("a[href='#article-comment'] span::text").ext
...: ract()
In []: comment_num
Out[]: [' 7 评论']
In []: comment_num = response.css("a[href='#article-comment'] span::text").ext
...: ract().strip()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input--18ae8761867f> in <module>()
----> comment_num = response.css("a[href='#article-comment'] span::text").extract().strip()
AttributeError: 'list' object has no attribute 'strip'
In []: comment_num = response.css("a[href='#article-comment'] span::text").ext
...: ract()[]
In []: comment_num
Out[]: ' 7 评论'
In []:
PS:css选择器里,不同标签使用空格隔开
第七篇 css选择器实现字段解析的更多相关文章
- css选择器优先级全解析
这样一个问题: <!doctype html> <htmllang="en"> <head> <metacharset="UTF ...
- 第七篇 CSS盒子
CSS盒子模型 在页面上,我们要控制元素的位置,比如:写作文一样,开头的两个字会空两个格子(这是在学校语文作文一样),其后就不会空出来,还有,一段文字后面跟着一张图,它们距离太近,不好看,我们要移 ...
- 网页抓取解析,使用JQuery选择器进行网页解析
最近开发一个小功能,数据库中一个基础表的数据从另一个网站采集. 因为网站的数据不定时更新,需要更新后自动采集最新的内容. 怎么判断更新数据没有? 好在网站有一个更新日志提示的地方,只需要对比本地保留的 ...
- 为什么排版引擎解析 CSS 选择器时一定要从右往左解析?
首先我们要看一下选择器的「解析」是在何时进行的. 主要参考这篇「 How browsers work」(http://taligarsiel.com/Projects/howbrowserswork1 ...
- 30个最常用css选择器解析(zz)
你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远远不是css的全部.下面向大家系统的解析css中30个最常用的选择器,包括我们最头痛的浏览器兼容性问题.掌握了它们,才能真正领 ...
- 30个最常用css选择器解析
转自:http://www.cnblogs.com/yiyuanke/archive/2011/10/22/CSS.html 你也许已经掌握了id.class.后台选择器这些基本的css选择器.但这远 ...
- 看这一篇就够了,css选择器知识汇总
对大多技术人员来说都比较熟悉CSS选择器,举一例子来说,假设给一个p标签增加一个类(class),可是执行后该class中的有些属性并没有起作用.通过Firebug查看,发现没有起作用的属性被覆盖了, ...
- 浏览器如何解析css选择器?
浏览器会『从右往左』解析CSS选择器. 我们知道DOM Tree与Style Rules合成为 Render Tree,实际上是需要将Style Rules附着到DOM Tree上, 因此需要根据选择 ...
- 第四篇、CSS选择器
<html> <head> <meta charset="UTF-8"> <title>CSS选择器</title> & ...
随机推荐
- Tomcat启动脚本(2)catalina.bat
@echo off rem Licensed to the Apache Software Foundation (ASF) under one or more rem contributor lic ...
- Docker 容器使用
Docker 客户端 docker 客户端非常简单 ,我们可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项. runoob@runoob:~# docker :~# doc ...
- 关于JS递归函数求斐波那契数列两种实现方法
百度已经解释的很详细了,但是不写注释还真是看不懂,递归,就直接套公式了,for循坏,我们就用EXCEL看一下规律 可以看到B是A+B的和,A往后就是B的值,所以我们需要第三个变量来保存他们的和,取出B ...
- Neo4j基础入门
图数据库基础知识 图数据库以图这种数据结构为基础,可以保存任意种类的数据,以下图为基础,简单介绍Neo4j中的几个简单概念: 1.节点(Nodes) 表示图数据库的实体(entities),代表图数据 ...
- android中的ContentProvider实现数据共享
为了在应用程序之间交换数据,android中提供了ContentProvider,ContentProvider是不同应用程序之间进行数据交换的标准API.当一个应用程序需要把自己的数据暴露给其他程序 ...
- 【转载】vue install报错run `npm audit fix` to fix them, or `npm audit` for details html
原链接https://www.jianshu.com/p/60591cfc6952 执行npm install 出现如下提醒 added 253 packages from 162 contribut ...
- Java 连接池的工作原理
什么是连接? 连接,是我们的编程语言与数据库交互的一种方式.我们经常会听到这么一句话“数据库连接很昂贵“. 有人接受这种说法,却不知道它的真正含义.因此,下面我将解释它究竟是什么.[如果你已经知道了, ...
- 【JDK1.8】Java HashMap实现细节
底层是用数组实现的 /** * The table, initialized on first use, and resized as * necessary. When allocated, len ...
- HTML5 worker计数器简单示例
效果图: index.html var w; // 开始 function startWorker() { if (typeof (Worker) !== "undefined") ...
- java统计一个子串在指定字符串中出现的次数
今天查着用了用String类里的几个方法,分享下代码 题目要求:统计一个子串在指定字符串中出现的次数( 提示java字串出现了6次) public class SearchSameString { p ...