在學習 Python 時,我嘗試組織各種方法來從 html 文檔中獲取特定的標籤。
HTML 示例代碼
假設有一個HTML文檔如下,我們來一一看看如何使用BeautifulSoup模塊解析具體的標籤。
from bs4 import BeautifulSoup
html = """
<!DOCTYPE html>
<html>
<head></head>
<body>
<span class='test1'>Content1</span>
<div class='test1'>Content2</div>
<div class='test1' id='target' name='sangminem'>Goal</div>
<div class='test2'>Content3</div>
</body>
</html>
"""
soup = BeautifulSoup(html, "html.parser")
目標是下面的標籤。
<div class='test1' id='target' name='sangminem'>Goal</div>
使用 find() 方法
print(soup.find('div',id='target')) #tag, id
print(soup.find('div',attrs={'id':'target'})) #tag, id as attribute value
print(soup.find('div',attrs={'name':'sangminem'})) #tag, name as attribute value
print(soup.find(attrs={'name':'sangminem'})) #name as attribute value
print(soup.find(attrs={'id':'target'})) #id as attribute value
這是輸出。
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
利用 find_all() 方法
print(soup.find_all('div')[1]) #tag
print(soup.find_all('div',class_=['test1'])[1]) #tag, class
print(soup.find_all('div',id='target')[0]) #tag, id
print(soup.find_all('div',attrs={'class':'test1'})[1]) #tag, class as attribute value
print(soup.find_all('div',attrs={'id':'target'})[0]) #tag, id as attribute value
print(soup.find_all('div',attrs={'name':'sangminem'})[0]) #name as attribute value
print(soup.find_all(class_=['test1'])[2]) #class
print(soup.find_all(id='target')[0]) #id
print(soup.find_all(attrs={'class':'test1'})[2]) #class as attribute value
print(soup.find_all(attrs={'id':'target'})[0]) #id as attribute value
print(soup.find_all(attrs={'name':'sangminem'})[0]) #name as attribute value
這是輸出。
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
利用 select_one() 方法
print(soup.select_one('div.test1#target')) #tag, class, id
print(soup.select_one('div#target')) #tag, id
print(soup.select_one('.test1#target')) #class, id
print(soup.select_one('#target')) #id
這是輸出。
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
使用 select() 方法
print(soup.select('.test1')[2]) #class
print(soup.select('div.test1')[1]) #tag, class
print(soup.select('div.test1#target')[0]) #tag, class, id
print(soup.select('div#target')[0]) #tag, id
print(soup.select('.test1#target')[0]) #class, id
print(soup.select('#target')[0]) #id
這是輸出。
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
<div class="test1" id="target" name="sangminem">Goal</div>
find() 方法和 select_one() 方法得到一個結果。
如果有多個滿足條件的結果,則只檢索第一個值。
find_all() 和 select() 方法以數組的形式獲取所有滿足條件的結果。
因此,為了最終得到想要的值,一定不能省略數組索引。
如果因為有多種方法而難以決定使用哪一種,我們建議使用 select() 方法。
它易於使用,實際上解析速度更快。