首先我们导入模块
from bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" id="p1"><b>The Dormouse's story</b><b>----2b</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"><span><b>Elsie</b></span></span>--alice</span></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>
<div>我是div</div>
"""
# 前提是安装了lxml以后,指定解析器
html = BeautifulSoup(html,'lxml')
# 输出格式化以后的dom文档 # print(html.prettify())
#获取第一个标签 print(html.find('html',recursive=False))
#获取标题信息 print(html.title)
# 标签名称 print(html.title.name) #获取标签的文本 print(html.title.string)
# 获取单个属性
print(html.p['id'])
#获取所有属性 print(html.p.attrs)#获取标签下所有文本
print(html.p.get)#这个连标签一起获得
print(html.p.get_text())#获取的纯文本
#fand 查找 name:b标签名 res = html.find(name = 'p',attrs={'name':'p1','id':'p1'})
#同过id查找 res = html.find(id="p1") print(res)#查找所有符合条件的标签
res = html.find_all(name=['a','b'])