BeautifulSoup 是一个用于解析 HTML 和 XML 文档的 Python 库,常用于网页爬虫。
安装 BeautifulSoup
pip install beautifulsoup4 requests
网页解析示例
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])
提示: 这是一个重要的概念,需要特别注意理解和掌握。
注意: 这是一个常见的错误点,请避免犯同样的错误。
评论
请 登录 后发表评论