from bs4 import BeautifulSoup
html_doc = """
<html><head><title>测试</title></head>
<body>
<p class="title"><b>BeautifulSoup库方法和属性</b></p>
<p class="description">详细介绍BeautifulSoup库的方法和属性。</p>
</body></html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
# 使用 find() 方法查找标签
title_tag = soup.find('title')
print(title_tag)
# 使用 text 属性获取文本内容
print(title_tag.text)
# 使用 find_all() 方法查找所有符合条件的标签
p_tags = soup.find_all('p')
for tag in p_tags:
print(tag.text)
# 使用 get() 方法获取标签的属性值
p_tag = soup.find('p', {'class': 'title'})
print(p_tag.get('class'))
# 使用 children 属性获取子标签
for child in p_tag.children:
print(child)
# 使用 parent 属性获取父标签
print(title_tag.parent)