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 os模块中的isfile()和isdir()函数均返回false问题解决方法
Feb 04 Python
python中Genarator函数用法分析
Apr 08 Python
Python读写ini文件的方法
May 28 Python
Python的Scrapy爬虫框架简单学习笔记
Jan 20 Python
python机器学习之决策树分类详解
Dec 20 Python
python实现机器人行走效果
Jan 29 Python
基于Python实现的微信好友数据分析
Feb 26 Python
python求平均数、方差、中位数的例子
Aug 22 Python
OpenCV+Python--RGB转HSI的实现
Nov 27 Python
pytorch中的inference使用实例
Feb 20 Python
浅谈Tensorflow加载Vgg预训练模型的几个注意事项
May 26 Python
浅析python字符串前加r、f、u、l 的区别
Jan 24 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 adodb连接带密码access数据库实例,测试成功
2008/05/14 PHP
laravel中的一些简单实用功能
2018/11/03 PHP
用js来定义浏览器中一个左右浮动元素相对于页面主体宽度的位置的函数
2012/01/21 Javascript
javascript 基础篇3 类,回调函数,内置对象,事件处理
2012/03/14 Javascript
jQuery插件开发基础简单介绍
2013/01/07 Javascript
用innerhtml提高页面打开速度的方法
2013/08/02 Javascript
javascript 表格内容排序 简单操作示例代码
2014/01/03 Javascript
JQuery简单实现锚点链接的平滑滚动
2015/05/03 Javascript
JS实现跟随鼠标立体翻转图片的方法
2015/05/04 Javascript
JavaScript的Backbone.js框架的一些使用建议整理
2016/02/14 Javascript
详解Backbone.js框架中的模型Model与其集合collection
2016/05/05 Javascript
jQuery插件ajaxFileUpload使用详解
2017/01/10 Javascript
几种响应式文字详解
2017/05/19 Javascript
bootstrap multiselect 多选功能实现方法
2017/06/05 Javascript
关于JavaScript的单双引号嵌套问题
2017/08/20 Javascript
Angular实现的简单查询天气预报功能示例
2017/12/27 Javascript
vue中使用腾讯云Im的示例
2020/10/23 Javascript
Vue使用CDN引用项目组件,减少项目体积的步骤
2020/10/30 Javascript
python抓取网页中图片并保存到本地
2015/12/01 Python
python目录与文件名操作例子
2016/08/28 Python
Python对列表中的各项进行关联详解
2017/08/15 Python
浅谈python迭代器
2017/11/08 Python
对Python中实现两个数的值交换的集中方法详解
2019/01/11 Python
Python:合并两个numpy矩阵的实现
2019/12/02 Python
用python拟合等角螺线的实现示例
2019/12/27 Python
使用PyTorch训练一个图像分类器实例
2020/01/08 Python
Python基础之字符串常见操作经典实例详解
2020/02/26 Python
设置jupyter中DataFrame的显示限制方式
2020/04/12 Python
使用Python Tkinter实现剪刀石头布小游戏功能
2020/10/23 Python
美国沃尔玛网上超市:Walmart
2020/08/14 全球购物
市场营销专业毕业生求职信
2014/03/26 职场文书
低碳生活的宣传标语
2014/06/23 职场文书
领导班子个人对照检查材料(群众路线)
2014/09/26 职场文书
个人欠款协议书范本2014
2014/11/02 职场文书
大学生学年个人总结
2015/02/15 职场文书
详解Python中*args和**kwargs的使用
2022/04/07 Python