Beautiful Soup (bs4) 是一个用于解析 HTML 和 XML 文档的 Python 库,常用于网页抓取(Web Scraping)。它能将复杂的文档转换为树形结构,并提供简单的方法导航、搜索和修改文档内容。
核心特性
自动编码处理
自动将输入文档转换为 Unicode 输出为 UTF-8,无需担心编码问题。灵活的解析器支持
支持多种解析器:html.parser
(Python 内置)lxml
(速度快,需额外安装)html5lib
(高容错性,生成标准 HTML5)
直观的文档导航
提供类似 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}")
注意事项
- 遵守 robots.txt:抓取前检查目标网站的爬虫协议。
- 设置请求头:模拟浏览器访问避免被屏蔽:
headers = {"User-Agent": "Mozilla/5.0"} response = requests.get(url, headers=headers)
- 错误处理:网络请求和解析需添加异常捕获:
try: # 解析代码 except AttributeError: # 处理标签不存在的情况
进阶学习
- 官方文档:Beautiful Soup Documentation
- 实战项目:商品价格监控、新闻聚合、搜索引擎爬虫
通过 Beautiful Soup,你可以高效地从网页中提取结构化数据,是 Python 数据采集的核心工具之一!