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 相关文章推荐
tensorflow实现KNN识别MNIST
Mar 12 Python
Python可变参数*args和**kwargs用法实例小结
Apr 27 Python
python操作excel的包(openpyxl、xlsxwriter)
Jun 11 Python
Python实现将Excel转换成xml的方法示例
Aug 25 Python
django框架模板语言使用方法详解
Jul 18 Python
python读写csv文件并增加行列的实例代码
Aug 01 Python
Python测试Kafka集群(pykafka)实例
Dec 23 Python
Python模块_PyLibTiff读取tif文件的实例
Jan 13 Python
tensorflow mnist 数据加载实现并画图效果
Feb 05 Python
Python如何使用PIL Image制作GIF图片
May 16 Python
Python实例方法、类方法、静态方法区别详解
Sep 05 Python
关于python中remove的一些坑小结
Jan 04 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制作unicode解码工具(unicode编码转换器)代码分享
2013/12/24 PHP
php 伪静态之IIS篇
2014/06/02 PHP
PHP CodeIgniter框架的工作原理研究
2015/03/30 PHP
Thinkphp实现短信验证注册功能
2016/10/18 PHP
Save a File Using a File Save Dialog Box
2007/06/18 Javascript
JavaScript中的对象化编程
2008/01/16 Javascript
细品javascript 寻址,闭包,对象模型和相关问题
2009/04/27 Javascript
js 弹出菜单/窗口效果
2011/10/30 Javascript
jQuery实现图片信息的浮动显示实例代码
2013/08/28 Javascript
js带按钮的提示框可供选择示例代码
2013/09/17 Javascript
instanceof和typeof运算符的区别详解
2014/01/06 Javascript
js生成动态表格并为每个单元格添加单击事件的方法
2014/04/14 Javascript
用Node.js通过sitemap.xml批量抓取美女图片
2015/05/28 Javascript
APP中javascript+css3实现下拉刷新效果
2016/01/27 Javascript
Bootstrap打造一个左侧折叠菜单的系统模板(一)
2016/05/17 Javascript
利用JS屏蔽页面中的Enter按键提交表单的方法
2016/11/25 Javascript
echarts3 使用总结(绘制各种图表,地图)
2017/01/05 Javascript
基于复选框demo(分享)
2017/09/27 Javascript
JS数组求和的常用方法实例小结
2019/01/07 Javascript
如何在Angular8.0下使用ngx-translate进行国际化配置
2019/07/24 Javascript
使用p5.js临摹动态图形
2019/10/23 Javascript
node实现mock-plugin中间件的方法
2019/12/25 Javascript
vue-cli中实现响应式布局的方法
2021/03/02 Vue.js
初步理解Python进程的信号通讯
2015/04/09 Python
Python中json格式数据的编码与解码方法详解
2016/07/01 Python
pytorch构建网络模型的4种方法
2018/04/13 Python
利用Python如何制作好玩的GIF动图详解
2018/07/11 Python
Python使用random.shuffle()打乱列表顺序的方法
2018/11/08 Python
详解css3 mask遮罩实现一些特效
2018/10/24 HTML / CSS
春秋航空官方网站:Spring Airlines
2017/09/27 全球购物
Luxplus瑞典:香水和美容护理折扣
2018/01/28 全球购物
大学生职业规划前言模板
2013/12/27 职场文书
职员竞岗演讲稿
2014/05/14 职场文书
2015年乡镇工作总结范文
2015/04/22 职场文书
python自动化测试通过日志3分钟定位bug
2021/11/20 Python
Nebula Graph解决风控业务实践
2022/03/31 MySQL