继上一篇BeautifulSoup的高级应用,主要解说的是contents children descendants string strings stripped_strings。本篇主要解说.parent .parents .next_sibling .previous_sibling .next_siblings .previous_siblings

本篇博客继续使用上篇的html页面内容:

html_doc = """
<html>
<head><title>The Dormouse's story</title></head>
<p class="title"><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>
</html>"""

继续分析文档树 ,每一个 tag或字符串都有父节点 :被包括在某个 tag中

.parent:

通过 .parent 属性来获取某个元素的父节点.在样例html文档中,标签是标签的父节点:

title_tag = soup.title
title_tag
# <title>The Dormouse's story</title>
title_tag.parent
# <head><title>The Dormouse's story</title></head>

文档title的字符串也有父节点:标签

title_tag.string.parent
# <title>The Dormouse's story</title>

文档的顶层节点比方的父节点是 BeautifulSoup 对象:

html_tag = soup.html
type(html_tag.parent)
# <class 'bs4.BeautifulSoup'>

BeautifulSoup 对象的 .parent 是None。

.parents

通过元素的.parents属性能够递归得到元素的全部父辈节点 , 以下的样例使用了 .parents方 法遍历了 标签到根节点 的全部节点:

link = soup.a
link
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
for parent in link.parents:
if parent is None:
print(parent)
else:
print(parent.name)
# p
# body
# html
# [document]
# None

兄弟节点

举例说明:

<a>
<b>text1</b>
<c>text2</c>
</a>

这里的b和c节点为兄弟节点

.next_sibling 和 .previous_sibling .:

在文档树中 ,使用 .next_sibling 和 .previous_sibling 属性来查询兄弟节点:

sibling_soup = BeautifulSoup("<a><b>text1</b><c>text2</c></b></a>")
sibling_soup.b.next_sibling
# <c>text2</c>
sibling_soup.c.previous_sibling
# <b>text1</b>

b 标签有.next_sibling 属性 ,可是没有 .previous_sibling 属性 ,由于 b标签在同级节点中是第一个 .同理 ,c标签有 .previous_sibling 属性 ,却没有 .next_sibling 属性 。

link = soup.a link
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
link.next_sibling
# u',\n'

注意:第一个a标签的next_sibling 属性值为 。\n

link.next_sibling.next_sibling
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>

第一个a标签的next_sibling的next_sibling 属性值为Lacie

.next_siblings.previous_siblings.

通过 .next_siblings 和 .previous_siblings 属性对当前节点的兄弟节点迭代输出:

for sibling in soup.a.next_siblings:
print(repr(sibling)) # u',\n'
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
# u' and\n'
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
# u'; and they lived at the bottom of a well.'
# None
for sibling in soup.find(id="link3").previous_siblings: print(repr(sibling))
# ' and\n'
# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
# u',\n'
# <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
# u'Once upon a time there were three little sisters; and their names were\n'
# None

回退和前进:

举例html例如以下:

<html><head><title>The Dormouse's story</title></head> <p class="title"><b>The Dormouse's story</b></p>

HTML 解析器把这段字符串转换成一连的事件 : “ 打开标签 ”加入一段字符串 ”,关闭 标签 ”,”打开

标签 ”, 等.Beautiful Soup提供了重现解析器初始化过程的方法

.next_element 和 .previous_element .

.next_element 属性指向解析过程中下一个被的对象 (字符串或 tag),结果可能 与 .next_sibling 同样 ,但一般是不一样的 .

last_a_tag = soup.find("a", id="link3")
last_a_tag
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
last_a_tag.next_sibling
# '; and they lived at the bottom of a well.'

但这个 标签的 .next_element 属性结果是在标签被解析之后的内容 ,不是标 签后的句子部分 ,应该是字符串 ”Tillie”:

last_a_tag.next_element
# u'Tillie'

.previous_element 属性刚好与.next_element 相反 ,它指向当前被解 析的对象的前一个解析对象 :

last_a_tag.previous_element
# u' and\n'
last_a_tag.previous_element.next_element
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

.next_elements 和 .previous_elements

通过 .next_elements 和 .previous_elements 的迭代器就能够向前或后訪问文档解析内容 ,就好像文档正在被解析一样 :

for element in last_a_tag.next_elements:                  print(repr(element))
# u'Tillie'
# u';\nand they lived at the bottom of a well.'
# u'\n\n'
# <p class="story">...</p>
# u'...'
# u'\n'
# None

下一篇 将解说一下BeautifulSoup的搜索文档树的高级方法。

最新文章

  1. JavaScript - 引用类型
  2. [C++]访问控制与继承(public,protect,private) 有时间再整理!!!
  3. Nginx SSL配置过程
  4. Java动态替换InetAddress中DNS的做法简单分析1
  5. 20150224&mdash;ASP.NET基础
  6. Spring-Mybatis 异常记录(1)
  7. 浅谈qmake之pro、pri、prf、prl文件
  8. String数组必须初始化之后才能赋值
  9. [转]SecureCRT右键粘贴的设置
  10. 编写CGI程序步骤
  11. Basic command and advice for memcached
  12. java方法重写和super关键字
  13. 【jdbc访问数据库获取执行sql转换json】
  14. intent和手势探测
  15. mysql和mariadb支持insert delayed的问题
  16. BZOJ2144跳跳棋——LCA+二分
  17. Linux内核模块编程之Helloworld(初级)
  18. java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor
  19. Oracle性能优化之oracle中常见的执行计划及其简单解释
  20. java多线程机制2(安全问题)

热门文章

  1. cmake模板
  2. dubbo Failed to check the status of the service com.user.service.UserService. No provider available for the service
  3. 简单谈谈MySQL优化利器-慢查询
  4. java判断string数组中是否包含某个元素
  5. easyui datagrid 动态加入、移除editor
  6. poj2385(dp)
  7. 杭电3501Calculation 2 欧拉函数
  8. 2015多校联合训练赛hdu 5301 Buildings 2015 Multi-University Training Contest 2 简单题
  9. OSGi 和 C++
  10. linux操作系统下完全删除oracle数据库