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 相关文章推荐
35个Python编程小技巧
Apr 01 Python
Python常见数据结构详解
Jul 24 Python
Python多线程编程(五):死锁的形成
Apr 05 Python
用Python给文本创立向量空间模型的教程
Apr 23 Python
Django自定义插件实现网站登录验证码功能
Apr 19 Python
Python实现获取命令行输出结果的方法
Jun 10 Python
利用Python2下载单张图片与爬取网页图片实例代码
Dec 25 Python
python实现将一个数组逆序输出的方法
Jun 25 Python
用Python shell简化开发
Aug 08 Python
Linux下升级安装python3.8并配置pip及yum的教程
Jan 02 Python
python实现从尾到头打印单链表操作示例
Feb 22 Python
如何利用Python动态模拟太阳系运转
Sep 04 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
PHP $_FILES函数详解
2011/03/09 PHP
解决更换PHP5.4以上版本后Dedecms后台登录空白问题的方法
2015/10/23 PHP
php微信公众平台示例代码分析(二)
2016/12/06 PHP
php使用ftp实现文件上传与下载功能
2017/07/21 PHP
CL vs ForZe BO5 第一场 2.13
2021/03/10 DOTA
Jquery Ajax的Get方式时需要注意URL地方
2011/04/07 Javascript
JS实现根据当前文字选择返回被选中的文字
2014/05/21 Javascript
javascript随机之洗牌算法深入分析
2014/06/07 Javascript
抛弃Nginx使用nodejs做反向代理服务器
2014/07/17 NodeJs
jQuery $命名冲突解决方案汇总
2014/11/13 Javascript
JavaScript中Number.MIN_VALUE属性的使用示例
2015/06/04 Javascript
JavaScript简单下拉菜单实例代码
2015/09/07 Javascript
轻松实现js图片预览功能
2016/01/18 Javascript
jQuery实现的分子运动小球碰撞效果
2016/01/27 Javascript
easyui combotree加载静态数据问题(选不上)解决方法
2016/12/26 Javascript
详谈js中标准for循环与foreach(for in)的区别
2017/11/02 Javascript
基于进程内通讯的python聊天室实现方法
2015/06/28 Python
简单学习Python time模块
2016/04/29 Python
python脚本作为Windows服务启动代码详解
2018/02/11 Python
Python3数据库操作包pymysql的操作方法
2018/07/16 Python
Python多线程编程之多线程加锁操作示例
2018/09/06 Python
python shell命令行中import多层目录下的模块操作
2020/03/09 Python
Python 字典一个键对应多个值的方法
2020/09/29 Python
python 高阶函数简单介绍
2021/02/19 Python
全球速卖通法国在线交易平台:AliExpress法国
2017/07/07 全球购物
Myprotein荷兰官网:欧洲第一运动营养品牌
2020/07/11 全球购物
什么是SCM(软件配置管理)
2014/08/16 面试题
村委会主任先进事迹
2014/01/15 职场文书
金融学专科生自我鉴定
2014/02/21 职场文书
《守株待兔》教学反思
2014/03/01 职场文书
我为自己代言广告词
2014/03/18 职场文书
体育运动口号
2014/06/09 职场文书
教师批评与自我批评(群众路线)
2014/10/15 职场文书
交通事故协议书范文
2014/10/23 职场文书
Redis分布式锁Redlock的实现
2021/08/07 Redis
Win10 和 Win11可以共存吗? win10/11产品生命周期/服务更新介绍
2021/11/21 数码科技