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 相关文章推荐
Python3实现的腾讯微博自动发帖小工具
Nov 11 Python
python 实现在Excel末尾增加新行
May 02 Python
解决phantomjs截图失败,phantom.exit位置的问题
May 17 Python
python 读取txt,json和hdf5文件的实例
Jun 05 Python
对python内置map和six.moves.map的区别详解
Dec 19 Python
python实现LBP方法提取图像纹理特征实现分类的步骤
Jul 11 Python
Python使用APScheduler实现定时任务过程解析
Sep 11 Python
python使用turtle库绘制奥运五环
Feb 24 Python
Python批量安装卸载1000个apk的方法
Apr 10 Python
浅谈django框架集成swagger以及自定义参数问题
Jul 07 Python
基于Python实现简单学生管理系统
Jul 24 Python
python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法
Nov 05 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
js和php邮箱地址验证的实现方法
2014/01/09 PHP
php中文验证码实现示例分享
2014/01/12 PHP
php实现的Captcha验证码类实例
2014/09/22 PHP
phpmyadmin下载、安装、配置教程
2017/05/16 PHP
Laravel中为什么不使用blpop取队列详析
2018/08/01 PHP
Laravel5.1框架注册中间件的三种场景详解
2019/07/09 PHP
PHP字符串和十六进制如何实现互相转换
2020/07/16 PHP
Windows Live的@live.com域名注册漏洞 利用代码
2006/12/27 Javascript
js之onload事件的一点使用心得
2013/08/14 Javascript
js实现具有高亮显示效果的多级菜单代码
2015/09/01 Javascript
jQuery禁用快捷键例如禁用F5刷新 禁用右键菜单等的简单实现
2016/08/31 Javascript
KnockoutJS 3.X API 第四章之数据控制流with绑定
2016/10/10 Javascript
jquery+ajax实现省市区三级联动效果简单示例
2017/01/04 Javascript
关于不同页面之间实现参数传递的几种方式讨论
2017/02/13 Javascript
jQuery模拟爆炸倒计时功能实例代码
2017/08/21 jQuery
Vue使用watch监听一个对象中的属性的实现方法
2019/05/10 Javascript
微信小程序实现的picker多级联动功能示例
2019/05/23 Javascript
JS前端知识点总结之内置对象,日期对象和定时器相关操作
2019/07/05 Javascript
nodejs中各种加密算法的实现详解
2019/07/11 NodeJs
将Python中的数据存储到系统本地的简单方法
2015/04/11 Python
Python引用模块和查找模块路径
2016/03/17 Python
Python用61行代码实现图片像素化的示例代码
2018/12/10 Python
元组列表字典(莫烦python基础)
2019/04/03 Python
python时间序列按频率生成日期的方法
2019/05/14 Python
如何基于Python获取图片的物理尺寸
2019/11/25 Python
Python3.7 基于 pycryptodome 的AES加密解密、RSA加密解密、加签验签
2019/12/04 Python
Python urlopen()和urlretrieve()用法解析
2020/01/07 Python
天网面试题
2013/04/07 面试题
拖鞋店创业计划书
2014/01/15 职场文书
中级会计职业生涯规划范文
2014/01/16 职场文书
行政文秘岗位职责范本
2014/02/10 职场文书
2014年秋季开学寄语
2014/08/02 职场文书
2014年行政部工作总结
2014/11/19 职场文书
精神文明建设汇报材料
2014/12/24 职场文书
学校党支部公开承诺书
2015/04/30 职场文书
Win11使用CAD卡顿或者致命错误怎么办?Win11无法正常使用CAD的解决方法
2022/07/23 数码科技