Python BeautifulSoup 库使用教程

文章目录

  • 简介
  • [安装 BeautifulSoup 库](#安装 BeautifulSoup 库)
  • [BeautifulSoup 库的导入](#BeautifulSoup 库的导入)
  • [BeautifulSoup 库依赖的解析库](#BeautifulSoup 库依赖的解析库)
  • [创建 BeautifulSoup 对象](#创建 BeautifulSoup 对象)
  • CSS选择器
    • 1、通过标签名查找
    • [2、通过 CSS 的类名查找](#2、通过 CSS 的类名查找)
    • [3、通过 Tag(标签) 的 id 查找](#3、通过 Tag(标签) 的 id 查找)
    • [4、通过 是否存在某个属性来查找](#4、通过 是否存在某个属性来查找)
    • [5、通过 某个标签是否存在某个属性来查找](#5、通过 某个标签是否存在某个属性来查找)
  • 获取标签里面的文字内容
  • 获取标签里面属性的内容

简介

BeautiSoup 库主要用来解析 HTML 格式的网络文件,通过解析文档为用户提供需要抓取的数据。

安装 BeautifulSoup 库

对于BeautifulSoup ,目前 最新版本是 4.x 版本,已经移植到 BS4中,Soup 3已经停止开发。

py 复制代码
pip install beautifulsoup4  -i  https://mirrors.aliyun.com/pypi/simple/

使用 pip list 查看是否安装成功

BeautifulSoup 库的导入

py 复制代码
from bs4 import BeautifulSoup

BeautifulSoup 库依赖的解析库

BeautifulSoup 在解析的时候实际上是需要依赖于解析器的,它除了支持 Python 标准库中的 HTML 解析器,还支持一些第三方的解析器。

常见解析器比较:

解析器 使用方法 优势 劣势
Python 标准库 BeautifulSoup (markup, "html.parser") 1、Python 的内置标准库 2、执行速度适中 3、文档容错能力强 Python 2.7.3 或 3.2.2 前的版本中 文档容错能力差
lxml HTML 解析器 BeautifulSoup (markup, "lxml") 1、速度快 2、文档容错能力强 需要安装C语言库
lxml XML 解析器 BeautifulSoup (markup, "xml") 1、速度快 2、唯一支持 XML 的解析器 需要安装C语言库
html5lib BeautifulSoup (markup, "html5lib") 1、最好的容错性 2、以浏览器的方式解析文档 3、生成HTML5 格式的文档 速度慢,不依赖外部拓展

创建 BeautifulSoup 对象

py 复制代码
soup = BeautifulSoup(markup, features)
  • markup :要解析的 HTML 格式的字符串
  • features :要使用的解析器类型
    • "html.parser"
    • "lxml"
    • "xml"
    • "html5lib"

CSS选择器

在CCS中,标签名不加任何修饰,类名前加点,ID名前加 # ,在这里我们也可以用类似的方法来筛选元素,用到的方法是 soup.select() , 返回类型是 list

示例 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
   <a class="sister" href="http://example.com/elsie" id="link1">
    <!--Elsie -->
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    <!--Lacie-->
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ;
and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</htm1>

1、通过标签名查找

通过标签名可以直接查找、逐层查找,也可以找到某个标记下的直接子标记和兄弟节点标签。
标签名不加任何修饰,类名前加点,ID名前加 #

直接查找 title 标记

print( soup.select( "title" ) )
output: [<titile>The Dormouse' s story</title>]

逐层查找 title 标记

print( soup.select( "html head title" ) )
output: [<titile>The Dormouse' s story</title>]

查找 直接子节点

查找 head 下的 title 标签

print( soup.select( "head title" ) )
output: [<titile>The Dormouse' s story</title>]

查找 p 下的 id="link1" 的标签

print( soup.select( "p #link1" ) )
output: [<a class="sister" href="http://example.com/elsie" id="link1">\ </a>]

查找 兄弟节点

查找 id="link1" 之后 class=sister 的所有兄弟标签

print( soup.select( "#link1 ~ .sister " ) )
output: [<a class="sister" href="http://example.com/lacie" id="link2"> <!--Lacie--> </a>,

<a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a>]

查找 紧跟着 id="link1" 之后 class=sister 的所有子标签

print( soup.select( "#link1 + .sister " ) )
output: [<a class="sister" href="http://example.com/lacie" id="link2"> <!--Lacie--> </a>]

2、通过 CSS 的类名查找

print( soup.select( ".sister" ) )
output: [<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie --></a>,

<a class="sister" href="http://example.com/lacie" id="link2"> <!--Lacie--> </a>,

<a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a>]

3、通过 Tag(标签) 的 id 查找

print( soup.select( "#link3" ) )
output: <a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a>]

4、通过 是否存在某个属性来查找

查找 是标签a 并且有 href 属性的Tag

print( soup.select( "a[href]" ) )
output: [<a class="sister" href="http://example.com/elsie" id="link1"><!--Elsie --></a>,

<a class="sister" href="http://example.com/lacie" id="link2"> <!--Lacie--> </a>,

<a class="sister" href="http://example.com/tillie" id="link3"> Tillie </a>]

5、通过 某个标签是否存在某个属性来查找

查找 是标签a 并且有 href 属性的Tag

print( soup.select( "a[href]" ) )

查找 是标签a 并且 id='link1' 的Tag

print( soup.select( "a[ id='link1' ]" ) )

获取标签里面的文字内容

py 复制代码
print( soup.title.string )

获取标签里面属性的内容

py 复制代码
# 打印 标签p 的属性
# 返回的是一个字典
print( soup.p.attrs )

# 获取 标签p 的class 属性值
print( soup.p['class'] )
相关推荐
刘一哥GIS12 分钟前
Windows环境搭建:PostGreSQL+PostGIS安装教程
数据库·python·arcgis·postgresql·postgis
西柚小萌新19 分钟前
【深入浅出PyTorch】--4.PyTorch基础实战
人工智能·pytorch·python
用户83562907805122 分钟前
掌控PDF页面:使用Python轻松实现添加与删除
后端·python
用户3721574261351 小时前
Python 实现 Excel 文件加密与保护
python
Derrick__11 小时前
Python访问数据库——使用SQLite
数据库·python·sqlite
总有刁民想爱朕ha1 小时前
AI大模型学习(17)python-flask AI大模型和图片处理工具的从一张图到多平台适配的简单方法
人工智能·python·学习·电商图片处理
小虎鲸002 小时前
PyTorch的安装与使用
人工智能·pytorch·python·深度学习
加油吧zkf2 小时前
Python入门:从零开始的完整学习指南
开发语言·前端·python
杰瑞学AI2 小时前
我的全栈学习之旅:FastAPI (持续更新!!!)
后端·python·websocket·学习·http·restful·fastapi
用户3721574261352 小时前
Python 高效实现 Excel 与 CSV 互转:用自动化提升效率
python