欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python爬虫:bs4遍历文档树

程序员文章站 2022-07-13 12:50:55
...
html_doc = '''
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="sister"><b>$37</b></p>

<p class="story" id="p">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" >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>
'''
from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc,'lxml')
print(soup)
print(type(soup))

# 遍历文档树
# *****1、直接使用
print(soup.html)
print(type(soup.html))
print(soup.a)
print(soup.p)

# *****2、获取标签的名称
print(soup.a.name)

# *****3、获取标签的属性
print(soup.a.attrs)

# 获取a标签中所有属性
print(soup.a.attrs['href'])

# ***** 4、获取标签的文本内容
print(soup.p.text)

# 5、嵌套选择
print(soup.html.body.p)

# 6、子节点、子孙节点
print(soup.p.children)
# 返回迭代器对象
print(list(soup.p.children))

# 7、父节点、祖先节点
print(soup.b.parent)
print(soup.b.parents)
print(list(soup.b.parents))

# 8、兄弟节点(sibling:兄弟姐妹)
print(soup.a)
# 获取下一个兄弟节点
print(soup.a.next_sibling)

# 获取下一个的所有兄弟节点,返回的是一个生成器
print(soup.a.next_siblings)
print(list(soup.a.next_siblings))

# 获取上一个兄弟节点
print(soup.a.previous_sibling)

# 获取上一个的所有兄弟节点,返回的是一个生成器
print(list(soup.a.previous_siblings))