在学习 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() 方法。
它易于使用,实际上解析速度更快。