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使用scrapy采集时伪装成HTTP/1.1的方法
Apr 08 Python
Python中输出ASCII大文字、艺术字、字符字小技巧
Apr 28 Python
python 回调函数和回调方法的实现分析
Mar 23 Python
Python3实现发送QQ邮件功能(html)
Dec 15 Python
Python操作json的方法实例分析
Dec 06 Python
对python同一个文件夹里面不同.py文件的交叉引用方法详解
Dec 15 Python
python opencv图片编码为h264文件的实例
Dec 12 Python
Django 实现将图片转为Base64,然后使用json传输
Mar 27 Python
Python持续监听文件变化代码实例
Jul 22 Python
Pycharm添加虚拟解释器报错问题解决方案
Oct 13 Python
详解Python中的进程和线程
Jun 23 Python
PyTorch中的torch.cat简单介绍
Mar 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
ThinkPHP实现事务回滚示例代码
2014/06/23 PHP
php简单socket服务器客户端代码实例
2015/05/18 PHP
Laravel最佳分割路由文件(routes.php)的方式
2016/08/04 PHP
PHP使用PDO实现mysql防注入功能详解
2019/12/20 PHP
thinkphp框架实现路由重定义简化url访问地址的方法分析
2020/04/04 PHP
PHP如何通过带尾指针的链表实现'队列'
2020/10/22 PHP
另一个javascript小测验(代码集合)
2011/07/27 Javascript
JavaScript闭包 懂不懂由你反正我是懂了
2011/10/21 Javascript
jQuery实现 注册时选择阅读条款 左右移动
2013/04/11 Javascript
Jquery实现点击切换图片并隐藏显示内容(2种方法实现)
2013/04/11 Javascript
jQuery表单获取和失去焦点输入框提示效果的实例代码
2013/08/01 Javascript
jquery使用slideDown实现模块缓慢拉出效果的方法
2015/03/27 Javascript
Angular Js文件上传之form-data
2015/08/28 Javascript
基于jquery实现图片上传本地预览功能
2016/01/08 Javascript
jQuery EasyUI Panel面板组件使用详解
2017/02/28 Javascript
基于vue2.0实现的级联选择器
2017/06/09 Javascript
angularjs之$timeout指令详解
2017/06/13 Javascript
Node.JS更改Windows注册表Regedit的方法小结
2017/08/18 Javascript
vuex操作state对象的实例代码
2018/04/25 Javascript
vue-router权限控制(简单方式)
2018/10/29 Javascript
elementUI table表格动态合并的示例代码
2019/05/15 Javascript
浅谈react-router@4.0 使用方法和源码分析
2019/06/04 Javascript
Vue.js 无限滚动列表性能优化方案
2019/12/02 Javascript
pyqt4教程之widget使用示例分享
2014/03/07 Python
Python编程中time模块的一些关键用法解析
2016/01/19 Python
python使用webdriver爬取微信公众号
2018/08/31 Python
详解pandas如何去掉、过滤数据集中的某些值或者某些行?
2019/05/15 Python
Python跑循环时内存泄露的解决方法
2020/01/13 Python
新媒传信软件测试面试题
2013/02/24 面试题
2014村务公开实施方案
2014/02/25 职场文书
承诺书模板
2014/08/30 职场文书
2014年酒店服务员工作总结
2014/12/08 职场文书
2015新年寄语大全
2014/12/08 职场文书
中班下学期个人工作总结
2015/02/12 职场文书
2019大学毕业晚会主持词
2019/06/21 职场文书
Minikube搭建Kubernetes集群
2022/03/31 Servers