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读写Redis数据库操作示例
Mar 18 Python
Python下rrdtool模块的基本使用方法
Nov 13 Python
Python实现随机生成有效手机号码及身份证功能示例
Jun 05 Python
分享一下如何编写高效且优雅的 Python 代码
Sep 07 Python
Python深度优先算法生成迷宫
Jan 22 Python
python实现淘宝秒杀聚划算抢购自动提醒源码
Jun 23 Python
python如何求解两数的最大公约数
Sep 27 Python
使用PIL(Python-Imaging)反转图像的颜色方法
Jan 24 Python
python time.sleep()是睡眠线程还是进程
Jul 09 Python
python2和python3实现在图片上加汉字的方法
Aug 22 Python
Python插入Elasticsearch操作方法解析
Jan 19 Python
python的Jenkins接口调用方式
May 12 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数组是否为空的代码
2011/09/08 PHP
PHP Switch 语句之学习笔记
2013/09/21 PHP
PHP的PDO事务与自动提交
2019/01/24 PHP
DOM 基本方法
2009/07/18 Javascript
jquery json 实例代码
2010/12/02 Javascript
基于jquery的动态创建表格的插件
2011/04/05 Javascript
js遍历、动态的添加数据的小例子
2013/06/22 Javascript
jQuery中的val()示例应用
2014/02/26 Javascript
对于Form表单reset方法的新认识
2014/03/05 Javascript
jQuery unbind 删除绑定事件详解
2016/05/24 Javascript
关于JS中setTimeout()无法调用带参函数问题的解决方法
2016/06/21 Javascript
js 中获取制定的cook信息实现方法
2016/11/19 Javascript
vue.js学习之递归组件
2016/12/13 Javascript
Vue 实现点击空白处隐藏某节点的三种方式(指令、普通、遮罩)
2019/10/23 Javascript
浅谈关于vue中scss公用的解决方案
2019/12/02 Javascript
微信小程序实现限制用户转发功能的实例代码
2020/02/22 Javascript
vue实现列表滚动的过渡动画
2020/06/29 Javascript
python smtplib模块发送SSL/TLS安全邮件实例
2015/04/08 Python
使用Python编写提取日志中的中文的脚本的方法
2015/04/30 Python
Python使用面向对象方式创建线程实现12306售票系统
2015/12/24 Python
Python读取文件内容的三种常用方式及效率比较
2017/10/07 Python
numpy ndarray 按条件筛选数组,关联筛选的例子
2019/11/26 Python
python pptx复制指定页的ppt教程
2020/02/14 Python
Python开发之pip安装及使用方法详解
2020/02/21 Python
tensorflow中tf.reduce_mean函数的使用
2020/04/19 Python
Python中的xlrd模块使用原理解析
2020/05/21 Python
python 5个顶级异步框架推荐
2020/09/09 Python
详解Python利用configparser对配置文件进行读写操作
2020/11/03 Python
pytorch 计算Parameter和FLOP的操作
2021/03/04 Python
毕业生自荐信
2013/12/14 职场文书
公司财务流程之主管工作流程
2014/03/03 职场文书
学生会主席演讲稿
2014/04/25 职场文书
机关作风整顿个人整改措施2014
2014/09/17 职场文书
2015年入党决心书
2015/02/05 职场文书
接待员岗位职责范本
2015/04/15 职场文书
MySQL 原理与优化之Limit 查询优化
2022/08/14 MySQL