目录:
安装依赖
$ pip3 install beautifulsoup4
解析文档
from bs4 import BeautifulSoup
# 示例HTML文档
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>
"""
# 创建 BeautifulSoup 对象,使用 html.parser 解析器
soup = BeautifulSoup(html_doc, 'html.parser')
# 使用 prettify() 方法美化输出 HTML 结构
print(soup.prettify())
上面示例默认使用 html.parser 解析器,如果文档很大,解析速度慢,可以考虑安装 lxml,并指定 lxml 作为解析器。
$ pip3 install lxml
from bs4 import BeautifulSoup
# 示例HTML文档
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>
"""
# 创建 BeautifulSoup 对象,指定使用 lxml 解析器
soup = BeautifulSoup(html_doc, 'lxml')
# 使用 prettify() 方法美化输出 HTML 结构
print(soup.prettify())
find() 查找第一个匹配结果,find_all() 查找所有匹配结果。
示例: 获取文本为 “保存” 的 button 按钮。
from bs4 import BeautifulSoup
html_doc = """
<html>
<body>
<button>取消</button>
<button>保存</button>
<button>删除</button>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
save_button = soup.find('button', string='保存')
print(save_button)
示例: 获取类名包含指定字符串的元素。由于 class 是 Python 中关键字,查找时使用 class_ 替代。
from bs4 import BeautifulSoup
import re
html_doc = """
<div class="WeeklyQueryForm_queryForm__8tfeV">
Form
</div>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
pattern = re.compile(r'queryForm')
element = soup.find(class_=pattern)
print(element)
select() 查找所有匹配元素,返回一个列表; select_one() 查找第一个匹配元素; 二者的参数都支持 css 选择器语法。
示例: 获取类名包含指定字符串的元素。
from bs4 import BeautifulSoup
html_doc = """
<div class="WeeklyQueryForm_queryForm__8tfeV">
Form
</div>
"""
# 创建 BeautifulSoup 对象,指定使用 lxml 解析器
soup = BeautifulSoup(html_doc, 'html.parser')
element = soup.select_one('[class*="queryForm"]')
print(element)
示例: 获取 a 标签的 href 属性值。
from bs4 import BeautifulSoup
# 示例HTML文档
html_doc = """
<div>
<a href="link1.html">第一个链接</a>
</div>
"""
# 创建 BeautifulSoup 对象,指定使用 lxml 解析器
soup = BeautifulSoup(html_doc, 'html.parser')
href = soup.find('a')['href']
print(href) # 输出: link1.html
示例: 获取 a 标签的文本值。
string 或者 text 属性都可以获取文本值。
from bs4 import BeautifulSoup
# 示例HTML文档
html_doc = """
<div>
<a href="link1.html">第一个链接</a>
</div>
"""
# 创建 BeautifulSoup 对象,指定使用 lxml 解析器
soup = BeautifulSoup(html_doc, 'html.parser')
text = soup.find('a').string
# 或者
# text = soup.find('a').text
print(text) # 输出: 第一个链接
↶ 返回首页 ↶