Beautiful Soup 是一個用于解析HTML和XML文檔的Python庫。它能夠將復雜的HTML文檔轉換為一個復雜的樹形結構,每個節點都是Python對象。Beautiful Soup 提供了簡單易用的方法來遍歷、搜索和修改文檔樹,使得從網頁中提取數據變得非常容易。
在使用Beautiful Soup之前,首先需要安裝它??梢酝ㄟ^以下命令使用pip進行安裝:
pip install beautifulsoup4
此外,Beautiful Soup 依賴于解析器,常用的解析器有 html.parser
、lxml
和 html5lib
。html.parser
是Python標準庫的一部分,無需額外安裝。如果需要使用 lxml
或 html5lib
,可以通過以下命令安裝:
pip install lxml
pip install html5lib
首先,我們需要將HTML文檔解析為Beautiful Soup對象。以下是一個簡單的例子:
from bs4 import BeautifulSoup
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
Beautiful Soup 提供了多種方法來查找標簽。最常用的方法是 find()
和 find_all()
。
find()
方法返回第一個匹配的標簽。find_all()
方法返回所有匹配的標簽。# 查找第一個 <p> 標簽
first_p = soup.find('p')
print(first_p)
# 查找所有 <p> 標簽
all_p = soup.find_all('p')
print(all_p)
可以使用 .string
或 .get_text()
方法來獲取標簽的內容。
# 獲取第一個 <p> 標簽的內容
first_p_text = first_p.string
print(first_p_text)
# 獲取所有 <p> 標簽的內容
all_p_text = [p.get_text() for p in all_p]
print(all_p_text)
可以使用 .get()
方法來獲取標簽的屬性。
# 獲取第一個 <a> 標簽的 href 屬性
first_a = soup.find('a')
href = first_a.get('href')
print(href)
Beautiful Soup 支持使用CSS選擇器來查找標簽??梢允褂?.select()
方法來使用CSS選擇器。
# 查找所有 class 為 "sister" 的 <a> 標簽
sisters = soup.select('a.sister')
print(sisters)
# 查找 id 為 "link2" 的 <a> 標簽
link2 = soup.select_one('#link2')
print(link2)
Beautiful Soup 還支持使用正則表達式來查找標簽??梢詫⒄齽t表達式傳遞給 find()
或 find_all()
方法。
import re
# 查找所有 href 屬性包含 "example.com" 的 <a> 標簽
example_links = soup.find_all('a', href=re.compile("example.com"))
print(example_links)
Beautiful Soup 提供了多種方法來遍歷文檔樹??梢允褂?.children
、.descendants
、.parent
、.next_sibling
等屬性來遍歷文檔樹。
# 遍歷第一個 <p> 標簽的所有子節點
for child in first_p.children:
print(child)
# 遍歷第一個 <p> 標簽的所有后代節點
for descendant in first_p.descendants:
print(descendant)
# 獲取第一個 <a> 標簽的父節點
parent = first_a.parent
print(parent)
# 獲取第一個 <a> 標簽的下一個兄弟節點
next_sibling = first_a.next_sibling
print(next_sibling)
Beautiful Soup 還允許修改文檔樹??梢孕薷臉撕灥膬热?、屬性,甚至添加或刪除標簽。
# 修改第一個 <a> 標簽的 href 屬性
first_a['href'] = 'http://example.com/new-link'
# 修改第一個 <p> 標簽的內容
first_p.string = 'New content'
# 添加一個新的 <a> 標簽
new_a = soup.new_tag('a', href="http://example.com/new")
new_a.string = 'New Link'
first_p.append(new_a)
# 刪除第一個 <a> 標簽
first_a.decompose()
print(soup.prettify())
以下是一個簡單的例子,演示如何使用Beautiful Soup爬取網頁的標題。
import requests
from bs4 import BeautifulSoup
url = 'http://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.title.string
print(title)
以下是一個例子,演示如何使用Beautiful Soup爬取網頁中的所有圖片鏈接。
import requests
from bs4 import BeautifulSoup
url = 'http://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
images = soup.find_all('img')
for img in images:
src = img.get('src')
print(src)
以下是一個例子,演示如何使用Beautiful Soup爬取網頁中的表格數據。
import requests
from bs4 import BeautifulSoup
url = 'http://example.com/table'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table')
rows = table.find_all('tr')
for row in rows:
cells = row.find_all('td')
data = [cell.get_text() for cell in cells]
print(data)
Beautiful Soup 會自動處理編碼問題,但有時可能需要手動指定編碼??梢允褂?response.encoding
來設置編碼。
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
Beautiful Soup 只能解析靜態HTML內容。如果需要處理動態加載的內容,可以使用Selenium等工具來模擬瀏覽器行為。
可以使用多線程或異步請求來提高爬蟲的效率。此外,可以使用緩存來避免重復請求。
Beautiful Soup 是一個功能強大且易于使用的Python庫,適用于從HTML和XML文檔中提取數據。通過掌握其基本用法和高級用法,可以輕松應對各種網頁爬取任務。希望本文能幫助你更好地理解和使用Beautiful Soup。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。