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中Continue语句的用法的举例详解
May 14 Python
python 系统调用的实例详解
Jul 11 Python
使用Eclipse如何开发python脚本
Apr 11 Python
对pandas中Series的map函数详解
Jul 25 Python
Python Selenium 之关闭窗口close与quit的方法
Feb 13 Python
python加密解密库cryptography使用openSSL生成的密匙加密解密
Feb 11 Python
pyecharts动态轨迹图的实现示例
Apr 17 Python
不到20行实现Python代码即可制作精美证件照
Apr 24 Python
python3用PyPDF2解析pdf文件,用正则匹配数据方式
May 12 Python
Matplotlib中rcParams使用方法
Jan 05 Python
Python WSGI 规范简介
Apr 11 Python
Python生成九宫格图片的示例代码
Apr 14 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
require(),include(),require_once()和include_once()的异同
2007/01/02 PHP
PHP实现异步调用方法研究与分享
2011/10/27 PHP
php 如何禁用eval() 函数实例详解
2016/12/01 PHP
一直复略了的一个问题,关于表单重复提交
2007/02/15 Javascript
锋利的jQuery 第三章章节总结的例子
2010/03/23 Javascript
jquery必须知道的一些常用特效方法及使用示例(整理)
2013/06/24 Javascript
Jquery Validate 正则表达式实用验证代码大全
2013/08/23 Javascript
一款由jquery实现的整屏切换特效
2014/09/15 Javascript
js实现点击链接后窗口缩小并居中的方法
2015/03/02 Javascript
jQuery实现表格展开与折叠的方法
2015/05/04 Javascript
根据输入邮箱号跳转到相应登录地址的解决方法
2016/12/13 Javascript
JQuery Dialog对话框 不能通过Esc关闭的原因分析及解决办法
2017/01/18 Javascript
Bootstrap学习笔记之进度条、媒体对象实例详解
2017/03/09 Javascript
前端图片懒加载(lazyload)的实现方法(提高用户体验)
2017/08/21 Javascript
Layui点击图片弹框预览的实现方法
2019/09/16 Javascript
layui 数据表格复选框实现单选功能的例子
2019/09/19 Javascript
vue相关配置文件详解及多环境配置详细步骤
2020/05/19 Javascript
原生js canvas实现鼠标跟随效果
2020/08/02 Javascript
Vue + ts实现轮播插件的示例
2020/11/10 Javascript
python操作MySQL数据库具体方法
2013/10/28 Python
Python 中 Virtualenv 和 pip 的简单用法详解
2017/08/18 Python
Python通过调用有道翻译api实现翻译功能示例
2018/07/19 Python
Python中修改字符串的四种方法
2018/11/02 Python
TensorFlow 多元函数的极值实例
2020/02/10 Python
python爬取网易云音乐热歌榜实例代码
2020/08/07 Python
golang/python实现归并排序实例代码
2020/08/30 Python
39美元购买一副眼镜或太阳镜:39DollarGlasses.com
2018/06/17 全球购物
TUMI香港官网:国际领先的行李箱、背囊品牌
2021/03/01 全球购物
可靠的数据流传输TCP
2016/03/15 面试题
女大学生毕业找工作的自我评价
2013/10/03 职场文书
业务副厂长岗位职责
2014/01/03 职场文书
五一服装活动方案
2014/01/11 职场文书
电视节目策划方案
2014/05/16 职场文书
2014年超市工作总结
2014/11/19 职场文书
领导激励员工的演讲稿,各种会上用得到,建议收藏
2019/08/13 职场文书
告别网页搜索!教你用python实现一款属于自己的翻译词典软件
2021/06/03 Python