Python Beautiful Soup 4【HTML/XML解析库】 简介

发布于:2025-06-21 ⋅ 阅读:(23) ⋅ 点赞:(0)

在这里插入图片描述


Beautiful Soup (bs4) 是一个用于解析 HTML 和 XML 文档的 Python 库,常用于网页抓取(Web Scraping)。它能将复杂的文档转换为树形结构,并提供简单的方法导航、搜索和修改文档内容。


核心特性
  1. 自动编码处理
    自动将输入文档转换为 Unicode 输出为 UTF-8,无需担心编码问题。

  2. 灵活的解析器支持
    支持多种解析器:

    • html.parser(Python 内置)
    • lxml(速度快,需额外安装)
    • html5lib(高容错性,生成标准 HTML5)
  3. 直观的文档导航
    提供类似 DOM 的操作方式,支持标签名、属性、CSS 选择器等搜索。


安装方法
pip install beautifulsoup4 requests  # 通常配合 requests 库使用

基础用法示例
from bs4 import BeautifulSoup
import requests

# 1. 获取网页内容
url = "https://example.com"
response = requests.get(url)
html_content = response.text

# 2. 创建 BeautifulSoup 对象
soup = BeautifulSoup(html_content, "html.parser")  # 使用内置解析器

# 3. 提取数据示例
# 获取标题
title = soup.title.string
print("页面标题:", title)

# 查找所有链接
for link in soup.find_all("a"):
    print("链接:", link.get("href"))

# 通过 CSS 类查找
results = soup.select(".main-content")  # 选择 class="main-content" 的元素
for div in results:
    print("内容块:", div.text.strip()[:50] + "...")  # 截取前50字符

常用方法速查
方法 描述
soup.find(tag) 返回第一个匹配的标签
soup.find_all(tag) 返回所有匹配的标签列表
soup.select(css_selector) 用 CSS 选择器查找元素
tag.get(attr) 获取标签属性值(如 href, src
tag.text 获取标签内的文本(不含子标签)
tag.contents 获取子节点列表
tag.parent 获取父节点

处理复杂场景
# 查找特定属性的元素
soup.find_all("div", class_="header", id="top")  # class 是保留字,需加下划线

# 链式查找
first_link = soup.find("div", {"id": "nav"}).find("a")

# 提取嵌套数据
for item in soup.select("ul.products > li"):
    name = item.find("h3").text
    price = item.select(".price")[0].text
    print(f"{name}: {price}")

注意事项
  1. 遵守 robots.txt:抓取前检查目标网站的爬虫协议。
  2. 设置请求头:模拟浏览器访问避免被屏蔽:
    headers = {"User-Agent": "Mozilla/5.0"}
    response = requests.get(url, headers=headers)
    
  3. 错误处理:网络请求和解析需添加异常捕获:
    try:
        # 解析代码
    except AttributeError:
        # 处理标签不存在的情况
    

进阶学习

通过 Beautiful Soup,你可以高效地从网页中提取结构化数据,是 Python 数据采集的核心工具之一!


网站公告

今日签到

点亮在社区的每一天
去签到