python爬虫之BeautifulSoup 使用select方法详解


Posted in Python onOctober 23, 2017

本文介绍了python爬虫之BeautifulSoup 使用select方法详解 ,分享给大家。具体如下:

<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list

(1)通过标签名查找

print soup.select('title') 
#[<title>The Dormouse's story</title>]
 
print soup.select('a')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
 
print soup.select('b')
#[<b>The Dormouse's story</b>]

(2)通过类名查找

print soup.select('.sister')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>, <a class="sister" href="http://example.com/lacie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

(3)通过 id 名查找

print soup.select('#link1')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

(4)组合查找

组合查找即和写 class 文件时,标签名与类名、id名进行的组合原理是一样的,例如查找 p 标签中,id 等于 link1的内容,二者需要用空格分开

print soup.select('p #link1')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

直接子标签查找

print soup.select("head > title")
#[<title>The Dormouse's story</title>]

(5)属性查找

查找时还可以加入属性元素,属性需要用中括号括起来,注意属性和标签属于同一节点,所以中间不能加空格,否则会无法匹配到。

print soup.select("head > title")
#[<title>The Dormouse's story</title>]
 
print soup.select('a[href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

同样,属性仍然可以与上述查找方式组合,不在同一节点的空格隔开,同一节点的不加空格

print soup.select('p a[href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ]')
#[<a class="sister" href="http://example.com/elsie" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1"><!-- Elsie --></a>]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python引用(import)文件夹下的py文件的方法
Aug 26 Python
Python中用函数作为返回值和实现闭包的教程
Apr 27 Python
Python实现的科学计算器功能示例
Aug 04 Python
python 实现数字字符串左侧补零的方法
Dec 04 Python
Python 文本文件内容批量抽取实例
Dec 10 Python
对python中的*args与**kwgs的含义与作用详解
Aug 28 Python
python实现文件的分割与合并
Aug 29 Python
python向图片里添加文字
Nov 26 Python
使用 pytorch 创建神经网络拟合sin函数的实现
Feb 24 Python
keras 读取多标签图像数据方式
Jun 12 Python
Python字典实现伪切片功能
Oct 28 Python
Django使用echarts进行可视化展示的实践
Jun 10 Python
浅谈python中copy和deepcopy中的区别
Oct 23 #Python
python的构建工具setup.py的方法使用示例
Oct 23 #Python
python使用pyqt写带界面工具的示例代码
Oct 23 #Python
基于Django的python验证码(实例讲解)
Oct 23 #Python
itchat接口使用示例
Oct 23 #Python
python实现微信接口(itchat)详细介绍
Oct 23 #Python
python爬虫_微信公众号推送信息爬取的实例
Oct 23 #Python
You might like
怎样搭建PHP开发环境
2015/07/28 PHP
laravel框架查询数据集转为数组的两种方法
2019/10/10 PHP
表单提交验证类
2006/07/14 Javascript
javascript 中对象的继承〔转贴〕
2007/01/22 Javascript
javascript 学习笔记(六)浏览器类型及版本信息检测代码
2011/04/08 Javascript
用Mootools获得操作索引的两种方法分享
2011/12/12 Javascript
在图片上显示左右箭头类似翻页的代码
2013/03/04 Javascript
关于JS中match() 和 exec() 返回值和属性的测试
2016/03/21 Javascript
jQuery Validate验证框架详解(推荐)
2016/12/17 Javascript
bootstrap的工具提示实例代码
2017/05/17 Javascript
在vue项目中正确使用iconfont的方法
2018/09/28 Javascript
VUE table表格动态添加一列数据,新增的这些数据不可以编辑(v-model绑定的数据不能实时更新)
2020/04/03 Javascript
为react组件库添加typescript类型提示的方法
2020/06/15 Javascript
[05:36]DOTA2 2015国际邀请赛中国区预选赛第四日TOP10
2015/05/29 DOTA
Python读取图片EXIF信息类库介绍和使用实例
2014/07/10 Python
Python实现全角半角转换的方法
2014/08/18 Python
Python判断变量是否为Json格式的字符串示例
2017/05/03 Python
利用信号如何监控Django模型对象字段值的变化详解
2017/11/27 Python
pandas删除行删除列增加行增加列的实现
2019/07/06 Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
2020/10/30 Python
利用 CSS3 实现的无缝轮播功能代码
2017/09/25 HTML / CSS
浅谈pc和移动端的响应式的使用
2019/01/03 HTML / CSS
html5 canvas里绘制椭圆并保持线条粗细均匀的技巧
2013/03/25 HTML / CSS
利用三角函数在canvas上画虚线的方法
2018/01/11 HTML / CSS
杭州联环马网络笔试题面试题
2013/08/04 面试题
营业员演讲稿
2013/12/30 职场文书
小学生倡议书范文
2014/05/13 职场文书
小学数学教学经验交流材料
2014/05/22 职场文书
早读课迟到检讨书
2014/09/25 职场文书
防灾减灾标语
2014/10/07 职场文书
作风建设年活动实施方案
2014/10/24 职场文书
党员个人承诺书
2015/04/27 职场文书
寻衅滋事罪辩护词
2015/05/21 职场文书
《水浒传》读后感3篇(范文)
2019/09/19 职场文书
CSS使用伪类控制边框长度的方法
2022/01/18 HTML / CSS
Java 深入探究讲解简单工厂模式
2022/04/07 Java/Android