BeautifulSoup四大属性

from bs4 import BeautifulSoup


html = 
'''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><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>
'''
# 1. BeautifulSoup 文档对象
soup = BeautifulSoup(html, 'lxml')

# 可以通过直接访问节点对象
# 2. Tag 对象 标签对象
# result=soup.title




# 3. NavigableString 文本内容
# 3.1内容
# result=soup.title.string


# 3.2 NavigableString 文本内容
# 属性attrs
# result=soup.p.attrs


# 4.Comment注释对象
# result=soup.a.string


# 获取所有节点可以使用 contents 或 children
# contents 返回列表
# result=soup.body.contents
# children返回是一个迭代器
# result=soup.body.children


# 获取属性 返回字典
# 属性
result=soup.p.attrs['name']

猜你喜欢

转载自blog.csdn.net/qq_41868948/article/details/81008558