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自然语言编码转换模块codecs介绍
Apr 08 Python
在Python的列表中利用remove()方法删除元素的教程
May 21 Python
Windows安装Python、pip、easy_install的方法
Mar 05 Python
Python编程实现的简单神经网络算法示例
Jan 26 Python
使用python获取csv文本的某行或某列数据的实例
Apr 03 Python
Python3 获取一大段文本之间两个关键字之间的内容方法
Oct 11 Python
Python基于opencv调用摄像头获取个人图片的实现方法
Feb 21 Python
为何人工智能(AI)首选Python?读完这篇文章你就知道了(推荐)
Apr 06 Python
Python中的asyncio代码详解
Jun 10 Python
python全栈知识点总结
Jul 01 Python
Pycharm新建模板默认添加个人信息的实例
Jul 15 Python
利用Vscode进行Python开发环境配置的步骤
Jun 22 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函数实现分页含文本分页和数字分页
2014/10/23 PHP
php实现对象克隆的方法
2015/06/20 PHP
PHP实现的简单sha1加密功能示例
2017/08/27 PHP
限制文本字节数js代码
2007/03/06 Javascript
javascript 硬盘序列号+其它硬件信息
2008/12/23 Javascript
js 实现在离开页面时提醒未保存的信息(减少用户重复操作)
2013/01/16 Javascript
extjs3 combobox取value和text案例详解
2013/02/06 Javascript
多引号嵌套的变量命名的问题
2014/05/09 Javascript
JS判断是否在微信浏览器打开的简单实例(推荐)
2016/08/24 Javascript
AngularJS 模块化详解及实例代码
2016/09/14 Javascript
ES6入门教程之let和const命令详解
2017/05/17 Javascript
Node.js pipe实现源码解析
2017/08/12 Javascript
React SSR样式及SEO的实践
2018/10/22 Javascript
详解Vue 动态组件与全局事件绑定总结
2018/11/11 Javascript
ionic3双击返回退出应用的方法
2019/09/17 Javascript
ElementUI之Message功能拓展详解
2019/10/18 Javascript
[01:02:18]VGJ.S vs infamous Supermajor 败者组 BO3 第一场 6.4
2018/06/05 DOTA
[05:23]DOTA2-DPC中国联赛2月1日Recap集锦
2021/03/11 DOTA
使用 Python 获取 Linux 系统信息的代码
2014/07/13 Python
线程和进程的区别及Python代码实例
2015/02/04 Python
简单了解python关系(比较)运算符
2019/07/08 Python
Python3直接爬取图片URL并保存示例
2019/12/18 Python
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
2020/06/30 Python
selenium自动化测试入门实战
2020/12/21 Python
目标责任书范本
2014/04/16 职场文书
小学亲子活动总结
2014/07/01 职场文书
2014教师党员个人自我评议
2014/09/20 职场文书
家长给老师的感谢信
2015/01/20 职场文书
安徽导游词
2015/02/12 职场文书
2015年酒店年度工作总结
2015/05/23 职场文书
教师节座谈会主持词
2015/07/03 职场文书
2015年教师节感言
2015/08/03 职场文书
有关信念的名言语录集锦
2019/12/06 职场文书
Django使用redis配置缓存的方法
2021/06/01 Redis
Pytorch中Softmax和LogSoftmax的使用详解
2021/06/05 Python
Win11任务栏无法正常显示 资源管理器不停重启的解决方法
2022/07/07 数码科技