Beautiful Soup 库入门

一、概念

Beautiful Soup 是一个可以从 HTML 或 XML 文件中提取数据的 Python 库。Beautiful Soup 提供一些简单的、python 式的函数用来处理导航、搜索、修改分析树等功能。它是一个工具箱,通过解析文档为用户提供需要抓取的数据。

Beautiful Soup 库官网

二、Beautiful Soup库的安装

Windows平台: “以管理员身份运行”cmd
执行pip install beautifulsoup4

测试一下:

# 代码
from bs4 import BeautifulSoup

demo = '''
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general‐purpose programming language. You can
learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT‐268001" class="py1" id="link1">Basic
Python</a> and <a href="http://www.icourse163.org/course/BIT‐1001870001" class="py2"
id="link2">Advanced Python</a>.</p>
</body></html>
'''

soup = BeautifulSoup(demo, 'html.parser')
print(soup.prettify())

结果如下:

<html>
 <head>
  <title>
   This is a python demo page
  </title>
 </head>
 <body>
  <p class="title">
   <b>
    The demo python introduces several python courses.
   </b>
  </p>
  <p class="course">
   Python is a wonderful general‐purpose programming language. You can
learn Python from novice to professional by tracking the following courses:
   <a class="py1" href="http://www.icourse163.org/course/BIT‐268001" id="link1">
    Basic
Python
   </a>
   and
   <a class="py2" href="http://www.icourse163.org/course/BIT‐1001870001" id="link2">
    Advanced Python
   </a>
   .
  </p>
 </body>
</html>

使用 Beautiful Soup 库主要就是下面两行代码:

from bs4 import BeautifulSoup
soup = BeautifulSoup('<p>data</p>', 'html.parser')

【注】demo 文本可以通过 requests 库方法获得:
这里写图片描述

三、Beautiful Soup 库的基本元素

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

四、BeautifulSoup 类的基本元素

这里写图片描述

1) Tag 标签:
任何存在于HTML语法中的标签都可以用 soup.tag 访问获得,当HTML文档中存在多个相同 tag 对应内容时,soup.tag 返回第一个。

这里写图片描述

2) Tag 的 name:
每个 tag 都有自己的名字,通过 tag.name 获取,字符串类型。

这里写图片描述

3) Tag 的 attrs (属性):

这里写图片描述

4) Tag 的 NavigableString:

这里写图片描述

5) Tag 的 Comment:

这里写图片描述

五、基于 bs4 库的 HTML 内容遍历方法

对于之前的 html 文本 demo:

demo = '''
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general‐purpose programming language. You can
learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT‐268001" class="py1" id="link1">Basic
Python</a> and <a href="http://www.icourse163.org/course/BIT‐1001870001" class="py2"
id="link2">Advanced Python</a>.</p>
</body></html>
'''

其 HTML 基本格式如下:

这里写图片描述

可以形成如下的标签树:

这里写图片描述

对于标签树,有下行遍历、上行遍历和平行遍历三种遍历方式:
【注】平行遍历只发生在同一个父节点下的各节点间。

这里写图片描述

1) 下行遍历:

这里写图片描述

这里写图片描述

2) 上行遍历:

这里写图片描述

示例代码:

from bs4 import BeautifulSoup

demo = '''
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general‐purpose programming language. You can
learn Python from novice to professional by tracking the following courses:
<a href="http://www.icourse163.org/course/BIT‐268001" class="py1" id="link1">Basic
Python</a> and <a href="http://www.icourse163.org/course/BIT‐1001870001" class="py2"
id="link2">Advanced Python</a>.</p>
</body></html>
'''

soup = BeautifulSoup(demo, "html.parser")
print(soup.name)
print(soup.parent)
for parent in soup.a.parents:
    print(parent.name)

"""
结果如下:
[document]
None
p
body
html
[document]
"""

soup 是根节点,没有父节点。

3) 平行遍历:

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

六、基于bs4库的HTML格式输出

这里写图片描述

这里写图片描述

这里写图片描述

【注】本文课件来自北京理工大学网络公开课:Python网络爬虫与信息提取

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Beautiful Soup 是一个Python,用于从HTML和XML文件中提取数据。它提供了一种简单的方式来遍历、搜索和修改解析树,让你能够轻松地从网页中提取所需的信息。 以下是 Beautiful Soup 的基础入门步骤: 1. 安装 Beautiful Soup: 你可以使用 pip 来安装 Beautiful Soup。在命令行中运行以下命令: ``` pip install beautifulsoup4 ``` 2. 导入 Beautiful Soup: 在你的 Python 脚本中导入 Beautiful Soup 模块: ```python from bs4 import BeautifulSoup ``` 3. 创建 Beautiful Soup 对象: 使用 BeautifulSoup 函数,将要解析的 HTML 或 XML 字符串作为参数传入,并指定解析器,例如 'html.parser': ```python soup = BeautifulSoup(html_doc, 'html.parser') ``` 4. 解析 HTML 结构: 你可以使用不同的 Beautiful Soup 方法来遍历解析树,从而提取数据。例如,你可以使用 `find` 方法来查找符合特定条件的单个元素,或者使用 `find_all` 方法来查找所有符合条件的元素。 ```python # 查找第一个 <p> 标签 paragraph = soup.find('p') # 查找所有 <a> 标签 links = soup.find_all('a') ``` 5. 提取数据: 一旦你找到了所需的元素,你可以使用 Beautiful Soup 提供的各种方法来提取其中的文本或属性。 ```python # 提取 <p> 标签的文本 paragraph_text = paragraph.get_text() # 提取 <a> 标签的 href 属性值 for link in links: href = link['href'] ``` 这只是 Beautiful Soup 的基础入门,它还有更多功能和用法。你可以参考官方文档来深入学习:https://www.crummy.com/software/BeautifulSoup/bs4/doc/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值