Beautiful选择器/遍历文档树Day3-7

#!/usr/bin/env python
#coding: utf8
#python2
#Beautiful选择器
from bs4 import BeautifulSoup
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>
"""

soup = BeautifulSoup(html_doc,'lxml')
#遍历文档树
#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.a.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.兄弟节点
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))

 

上一篇:爬虫---Beautiful Soup 爬取知乎热榜


下一篇:vscode ----无敌装B神器