Python BS4库的安装与使用详解


Posted in Python onAugust 08, 2018

Beautiful Soup 库一般被称为bs4库,支持Python3,是我们写爬虫非常好的第三方库。因用起来十分的简便流畅。所以也被人叫做“美味汤”。目前bs4库的最新版本是4.60。下文会介绍该库的最基本的使用,具体详细的细节还是要看:[官方文档](Beautiful Soup Documentation)

bs4库的安装

Python的强大之处就在于他作为一个开源的语言,有着许多的开发者为之开发第三方库,这样我们开发者在想要实现某一个功能的时候,只要专心实现特定的功能,其他细节与基础的部分都可以交给库来做。bs4库 就是我们写爬虫强有力的帮手。

安装的方式非常简单:我们用pip工具在命令行里进行安装

$ pip install beautifulsoup4

接着我们看一下是否成功安装了bs4库

$ pip list

这样我们就成功安装了 bs4 库

Python BS4库的安装与使用详解

bs4库的简单使用

这里我们先简单的讲解一下bs4库的使用,

暂时不去考虑如何从web上抓取网页,

假设我们需要爬取的html是如下这么一段:

下面的一段HTML代码将作为例子被多次用到.这是 爱丽丝梦游仙境的 的一段内容(以后内容中简称为 爱丽丝 的文档):

<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
  
<p class="story">Once upon a time there were three little sisters; and their names were
http://example.com/elsie" class="sister" id="link1">Elsie,
http://example.com/lacie" class="sister" id="link2">Lacie and
http://example.com/tillie" class="sister" id="link3">Tillie;
and they lived at the bottom of a well.</p>
  
<p class="story">...</p>
</html>

下面我们开始用bs4库解析这一段html网页代码。

#导入bs4模块
from bs4 import BeautifulSoup
#做一个美味汤
soup = BeautifulSoup(html,'html.parser')
#输出结果
print(soup.prettify())
  
'''
OUT:
  
# <html>
# <head>
#  <title>
#  The Dormouse's story
#  </title>
# </head>
# <body>
#  <p class="title">
#  <b>
#   The Dormouse's story
#  </b>
#  </p>
#  <p class="story">
#  Once upon a time there were three little sisters; and their names were
#  <a class="sister" href="http://example.com/elsie" rel="external nofollow" id="link1">
#   Elsie
#  </a>
#  ,
#  <a class="sister" href="http://example.com/lacie" rel="external nofollow" id="link2">
#   Lacie
#  </a>
#  and
#  <a class="sister" href="http://example.com/tillie" rel="external nofollow" id="link2">
#   Tillie
#  </a>
#  ; and they lived at the bottom of a well.
#  </p>
#  <p class="story">
#  ...
#  </p>
# </body>
# </html>
'''

可以看到bs4库将网页文件变成了一个soup的类型,

事实上,bs4库 是解析、遍历、维护、“标签树“的功能库。

通俗一点说就是: bs4库把html源代码重新进行了格式化,

从而方便我们对其中的节点、标签、属性等进行操作。

下面是几个简单的浏览结构化数据的方式 :

请仔细观察最前面的html文件

# 找到文档的title
soup.title
# <title>The Dormouse's story</title>
  
#title的name值
soup.title.name
# u'title'
  
#title中的字符串String
soup.title.string
# u'The Dormouse's story'
  
#title的父亲节点的name属性
soup.title.parent.name
# u'head'
  
#文档的第一个找到的段落
soup.p
# <p class="title"><b>The Dormouse's story</b></p>
  
#找到的p的class属性值
soup.p['class']
# u'title'
  
#找到a标签
soup.a
# http://example.com/elsie" id="link1">Elsie
  
#找到所有的a标签
soup.find_all('a')
# [http://example.com/elsie" id="link1">Elsie,
# http://example.com/lacie" id="link2">Lacie,
# http://example.com/tillie" id="link3">Tillie]
  
#找到id值等于3的a标签
soup.find(id="link3")
# http://example.com/tillie" id="link3">Tillie

通过上面的例子 我们知道bs4库是这样理解一个html源文件的:

首先 把html源文件转换为soup类型

接着 从中通过特定的方式抓取内容

 更高级点的用法?

从文档中找到所有<a>标签的链接:

#发现了没有,find_all方法返回的是一个可以迭代的列表
for link in soup.find_all('a'):
  print(link.get('href'))
  # http://example.com/elsie
  # http://example.com/lacie
  # http://example.com/tillie

从文档中获取所有文字内容:

#我们可以通过get_text 方法 快速得到源文件中的所有text内容。
print(soup.get_text())
# The Dormouse's story
#
# The Dormouse's story
#
# Once upon a time there were three little sisters; and their names were
# Elsie,
# Lacie and
# Tillie;
# and they lived at the bottom of a well.
#
# ...

bs4库的入门使用我们就先进行到这。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中用于返回绝对值的abs()方法
May 14 Python
浅谈Python类的__getitem__和__setitem__特殊方法
Dec 25 Python
理解python中生成器用法
Dec 20 Python
对python中的pop函数和append函数详解
May 04 Python
python整小时 整天时间戳获取算法示例
Feb 20 Python
10分钟教你用python动画演示深度优先算法搜寻逃出迷宫的路径
Aug 12 Python
pytorch中tensor.expand()和tensor.expand_as()函数详解
Dec 27 Python
已安装tensorflow-gpu,但keras无法使用GPU加速的解决
Feb 07 Python
PyCharm最新激活码PyCharm2020.2.3有效
Nov 18 Python
python使用scapy模块实现ping扫描的过程详解
Jan 21 Python
手把手教你配置JupyterLab 环境的实现
Feb 02 Python
python实现简单的三子棋游戏
Apr 28 Python
python特性语法之遍历、公共方法、引用
Aug 08 #Python
用Python shell简化开发
Aug 08 #Python
在Python中使用gRPC的方法示例
Aug 08 #Python
Python实现购物评论文本情感分析操作【基于中文文本挖掘库snownlp】
Aug 07 #Python
python实现彩票系统
Jun 28 #Python
django框架自定义用户表操作示例
Aug 07 #Python
Python实现基于POS算法的区块链
Aug 07 #Python
You might like
计数器详细设计
2006/10/09 PHP
escape unescape的php下的实现方法
2007/04/27 PHP
[原创]php使用strpos判断字符串中数字类型子字符串出错的解决方法
2017/04/01 PHP
一个报数游戏js版(约瑟夫环问题)
2010/08/05 Javascript
浅谈javascript中的作用域
2012/04/07 Javascript
jquery cookie的用法总结
2013/11/18 Javascript
js(JavaScript)实现TAB标签切换效果的简单实例
2014/02/26 Javascript
js操作模态窗口及父子窗口间相互传值示例
2014/06/09 Javascript
简单实现异步编程promise模式
2015/07/31 Javascript
13个PHP函数超实用
2015/10/21 Javascript
使用jQuery监听DOM元素大小变化
2016/02/24 Javascript
快速掌握Node.js事件驱动模型
2016/03/21 Javascript
js求数组中全部数字可拼接出的最大整数示例代码
2017/08/25 Javascript
动态统计当前输入内容的字节、字符数的实例详解
2017/10/27 Javascript
ES6 对象的新功能与解构赋值介绍
2019/02/05 Javascript
Angular2使用SVG自定义图表(条形图、折线图)组件示例
2019/05/10 Javascript
layui table 多行删除(id获取)的方法
2019/09/12 Javascript
基于vue项目设置resolves.alias: '@'路径并适配webstorm
2020/12/02 Vue.js
从零学python系列之从文件读取和保存数据
2014/05/23 Python
python实现下载文件的三种方法
2017/02/09 Python
利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例
2017/08/08 Python
vue.js实现输入框输入值内容实时响应变化示例
2018/07/07 Python
使用Python处理BAM的方法
2018/09/28 Python
基于plt.title无法显示中文的快速解决
2020/05/16 Python
HTML4和HTML5之间除了相似以外的10个主要不同
2012/12/13 HTML / CSS
兰蔻加拿大官方网站:Lancome加拿大
2016/08/05 全球购物
全球虚拟主机商:HostGator
2017/02/06 全球购物
教育系毕业生中文求职信范文
2013/10/06 职场文书
咨询公司各岗位职责
2013/12/02 职场文书
青年文明号事迹材料
2014/01/18 职场文书
平安工地汇报材料
2014/08/19 职场文书
材料采购员岗位职责
2015/04/03 职场文书
结婚当天新郎保证书
2015/05/08 职场文书
高中体育课教学反思
2016/02/16 职场文书
创业计划书之情侣餐厅
2019/09/29 职场文书
详解Python牛顿插值法
2021/05/11 Python