
正文
第七篇 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选择器里,不同标签使用空格隔开







