热门课程 - 发现知识之美

Python 编程基础

作者: 李老师 更新: 2025-11-26 阅读: 难度: 高级
学习工具

. BeautifulSoup 网页解析

BeautifulSoup 是一个用于解析 HTML 和 XML 文档的 Python 库,常用于网页爬虫。

安装 BeautifulSoup

安装命令
pip install beautifulsoup4 requests

网页解析示例

BeautifulSoup 使用
from bs4 import BeautifulSoup import requests # 获取网页内容 url = "https://example.com" response = requests.get(url) html_content = response.text # 创建 BeautifulSoup 对象 soup = BeautifulSoup(html_content, "html.parser") # 查找元素 title = soup.find("title").text print("网页标题:", title) # 查找所有链接 links = soup.find_all("a") for link in links: href = link.get("href") text = link.text.strip() if href and text: print(f"链接: {text} -> {href}") # 按类名查找 divs = soup.find_all("div", class_="content") for div in divs: print("内容:", div.text[:100])
提示: 这是一个重要的概念,需要特别注意理解和掌握。
注意: 这是一个常见的错误点,请避免犯同样的错误。

评论

头像
sf 2025-12-16 13:43:45
1
头像
admin 2025-12-16 13:00:40
这头像
头像
sun8388 2025-12-16 12:30:27
ggg

登录 后发表评论