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中引用与复制用法实例分析
Jun 04 Python
python实现文件路径和url相互转换的方法
Jul 06 Python
使用Python保存网页上的图片或者保存页面为截图
Mar 05 Python
Python实现批量读取图片并存入mongodb数据库的方法示例
Apr 02 Python
Python Requests库基本用法示例
Aug 20 Python
python搜索包的路径的实现方法
Jul 19 Python
基于python操作ES实例详解
Nov 16 Python
python判断链表是否有环的实例代码
Jan 31 Python
Python 从attribute到property详解
Mar 05 Python
vue常用指令代码实例总结
Mar 16 Python
python 连续不等式语法糖实例
Apr 15 Python
python中线程和进程有何区别
Jun 17 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 利用Mail_MimeDecode类提取邮件信息示例
2014/01/26 PHP
PHP格式化MYSQL返回float类型的方法
2016/03/30 PHP
Yii2中添加全局函数的方法分析
2017/05/04 PHP
三个思路解决laravel上传文件报错:413 Request Entity Too Large问题
2017/11/13 PHP
深入理解JavaScript系列(4) 立即调用的函数表达式
2012/01/15 Javascript
构造函数+原型模式构造js自定义对象(最通用)
2014/05/12 Javascript
jQuery+Ajax实现无刷新分页
2015/10/30 Javascript
跨域资源共享 CORS 详解
2016/04/26 Javascript
Node.js Streams文件读写操作详解
2016/07/04 Javascript
javaScript如何跳出多重循环break、continue
2016/09/01 Javascript
探讨跨域请求资源的几种方式(总结)
2016/12/02 Javascript
利用Node.js+Koa框架实现前后端交互的方法
2017/02/27 Javascript
JS利用cookies设置每隔24小时弹出框
2017/04/20 Javascript
jquery拖动改变div大小
2017/07/04 jQuery
vue中动态添加class类名的方法
2018/09/05 Javascript
Windows下支持自动更新的Electron应用脚手架的方法
2018/12/24 Javascript
JavaScript ES6箭头函数使用指南
2018/12/30 Javascript
实例详解带参数的 npm script
2019/05/28 Javascript
layui当点击文本框时弹出选择框,显示选择内容的例子
2019/09/02 Javascript
layui监听select变化,以及设置radio选中的方法
2019/09/24 Javascript
JavaScript冒泡算法原理与实现方法深入理解
2020/06/04 Javascript
Vue如何实现变量表达式选择器
2021/02/18 Vue.js
python psutil模块使用方法解析
2019/08/01 Python
pytorch索引查找 index_select的例子
2019/08/18 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
TensorFlow2.X使用图片制作简单的数据集训练模型
2020/04/08 Python
Python通过fnmatch模块实现文件名匹配
2020/09/30 Python
Zavvi荷兰:英国大型音像制品和图书游戏零售商
2018/03/22 全球购物
民族团结先进个人事迹材料
2014/06/02 职场文书
信息管理专业自荐书
2014/06/05 职场文书
多媒体编辑专业毕业生求职信
2014/06/13 职场文书
销售员试用期自我评价
2014/09/15 职场文书
校长四风对照检查材料
2014/09/27 职场文书
医院科室评语
2015/01/04 职场文书
村干部任职承诺书
2015/01/21 职场文书
电力企业职工培训心得体会
2016/01/11 职场文书