Beautiful Soup 仅用于解析 HTML 或 XML 文档,并为您提供易于导航的方法。要从 Beautiful Soup 对象中获取数据,请使用对相应标记/标签及其属性的引用。例如,要获取以标记“span”和类“my-class”表示的元素的文本,可以使用以下代码:
from bs4 import BeautifulSoup
html_doc = """
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
"""
soup = BeautifulSoup(html_doc, 'html.parser')
my_element = soup.find('span', {'class': 'my-class'})
if my_element:
my_text = my_element.text
print(my_text)
请注意,在此示例中,使用了“find”方法在 soup 对象中查找所有具有标记“span”和类“my-class”的元素。如果找到相应的元素,则使用“text”属性访问其文本内容。如果未找到元素,则变量“my_element”将为 None。