使用 python 和 beautifulsoup 解析 html 文檔的方法如下:加載 html 文檔并創建 beautifulsoup 對象。使用 beautifulsoup 對象查找和處理標簽元素,如:查找特定標簽:soup.find(tag_name)查找所有特定標簽:soup.find_all(tag_name)查找具有特定屬性的標簽:soup.find(tag_name, {‘attribute’: ‘value’})提取標簽的文本內容或屬性值。根據需要調整代碼以獲取特定信息。
使用 Python 和 BeautifulSoup 解析 HTML 文檔
目標:
了解如何使用 Python 和 BeautifulSoup 庫解析 HTML 文檔。
必備知識:
Python 基礎
HTML 和 XML 知識
代碼:
from bs4 import BeautifulSoup
# 加載 HTML 文檔
html_doc = """
<html>
<head>
<title>HTML 文檔</title>
</head>
<body>
<h1>標題</h1>
<p>段落</p>
</body>
</html>
"""
# 創建 BeautifulSoup 對象
soup = BeautifulSoup(html_doc, 'html.parser')
# 獲取標題標簽
title_tag = soup.find('title')
print(title_tag.text) # 輸出:HTML 文檔
# 獲取所有段落標簽
paragraph_tags = soup.find_all('p')
for paragraph in paragraph_tags:
print(paragraph.text) # 輸出:段落
# 獲取特定屬性的值
link_tag = soup.find('link', {'rel': 'stylesheet'})
print(link_tag['href']) # 輸出:樣式表鏈接
登錄后復制
實戰案例:
一個簡單的實戰案例是使用 BeautifulSoup 從網頁中提取指定信息的爬蟲。例如,你可以使用以下代碼從 Stack Overflow 中提取問題和答案:
import requests
from bs4 import BeautifulSoup
url = 'https://stack<a style='color:#f60; text-decoration:underline;' href="https://www.php.cn/zt/72718.html" target="_blank">overflow</a>.com/questions/31207139/using-beautifulsoup-to-extract-specific-attribute'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
questions = soup.find_all('div', {'class': 'question-summary'})
for question in questions:
question_title = question.find('a', {'class': 'question-hyperlink'}).text
question_body = question.find('div', {'class': 'question-snippet'}).text
print(f'問題標題:{question_title}')
print(f'問題內容:{question_body}')
print('---')
登錄后復制
這只是使用 BeautifulSoup 解析 HTML 文檔的眾多示例之一。你可以根據具體需求調整代碼以獲取不同的信息。






