1.直接子节点:.contents .children属性

.content

Tag的.content属性可以将Tag的子节点以列表的方式输出

from bs4 import BeautifulSouphtml = """
<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># 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")# 输出方式为列表
print(soup.head.contents)print(soup.head.contents[0])

运行结果

[<title>The Dormouse's story</title>]
<title>The Dormouse's story</title>

.children

它返回的不是一个列表,不过我们可以通过遍历获取所有的子节点。

from bs4 import BeautifulSouphtml = """
<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>
"""# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")# 输出方式为列表生成器对象
print(soup.head.children)# 通过遍历获取所有子节点
for child in soup.head.children:print(child)

运行结果

<list_iterator object at 0x008FF950>
<title>The Dormouse's story</title>

2.所有子孙节点:.descendants属性

上面讲的.contents.children属性仅包含Tag的直接子节点,.descendants属性可以对所有Tag的子孙节点进行递归循环,和children类似,我们也需要通过遍历的方式获取其中的内容。

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
from bs4 import BeautifulSouphtml = """
<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>
"""# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")# 输出方式为列表生成器对象
print(soup.head.descendants)# 通过遍历获取所有子孙节点
for child in soup.head.descendants:print(child)

运行结果

<generator object descendants at 0x00519AB0>
<title>The Dormouse's story</title>
The Dormouse's story

3.节点内容:.string属性

如果Tag只有一个NavigableString类型子节点,那么这个Tag可以使用.string得到子节点。如果一个Tag仅有一个子节点,那么这个Tab也可以使用.string方法,输出结果与当前唯一子节点的.string结果相同。

通俗点来讲就是:如果一个标签里面没有标签了,那么.string就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么.string也会返回里面的内容。例如:

from bs4 import BeautifulSouphtml = """
<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>
"""# 创建 Beautiful Soup 对象,指定lxml解析器
soup = BeautifulSoup(html, "lxml")print(soup.head.string)print(soup.head.title.string)

运行结果

The Dormouse's story
The Dormouse's story

Python爬虫遍历文档树相关推荐

  1. python爬虫:BeautifulSoup_遍历文档树

    前提.回顾 1.因为最近工作中都是在跟XML格式的报文打交道:主要就是XML报文的解析.入库.在做自动化时,需要解析XML报文,前面虽然学习过下BeautifulSoup,结果这次在写脚本时,突然发现 ...

  2. python 打印xml文档树_[Python]xml.etree.ElementTree处理xml文档

    需求: 在实际应用中,需要对xml配置文件进行实时修改, 1.增加.删除 某些节点 2.增加,删除,修改某个节点下的某些属性 3.增加,删除,修改某些节点的文本 xml源文件格式[例] path=&q ...

  3. python爬虫学习文档整理

    python爬虫文档收集: 1.正则表达式 2.Beautiful Soup4 3.Requests 4.Selenium with Pathon 5.Webdriver 6.Python3.6

  4. python 打印xml文档树_Python构建XML树结构的方法示例

    本文实例讲述了Python构建XML树结构的方法.分享给大家供大家参考,具体如下: 1.构建XML元素 #encoding=utf-8 from xml.etree import ElementTre ...

  5. python爬虫urllib文档_11.【文本】Urllib(下) - 零基础学习Python爬虫系列

    本文是视频av20148524的相关代码文档 # urllib(下) # post # post 和 get 传递参数同时存在的一个url url = "http://bbs.mumayi. ...

  6. Python 爬虫利器 Beautiful Soup 4 之文档树的搜索

    前面两篇介绍的是 Beautiful Soup 4 的基本对象类型和文档树的遍历, 本篇介绍 Beautiful Soup 4 的文档搜索 搜索文档树主要使用两个方法 find() 和 find_al ...

  7. Python : Beautiful Soup修改文档树

    修改文档树 Beautiful Soup的强项是文档树的搜索,但同时也可以方便的修改文档树 修改tag的名称和属性 在 Attributes 的章节中已经介绍过这个功能,但是再看一遍也无妨. 重命名一 ...

  8. python替换word内容,文档翻译-使用python替换word文档中的段落内容

    前段时间遇到一个需求,需要将word文档中的内容进行替换,并且需要保证格式不变.在找了一圈资料后,发现没有现成的api供使用:由于本人做过一段时间文档解析,因此打算从word文档的xml入手,通过py ...

  9. python - 官方简易文档篇(1)常用、函数

    Python Tutorial, 发布 3.8.4rc1 tutorial.pdf 刚总结完str的一些细节,其中还有很多关于类的自定义的因为没有接触过,所以还不知道如何去用,但是再菜鸟教程上看到一个 ...

最新文章

  1. C语言 数据类型(就做个笔记总结)
  2. 全球及中国管道运输行业建设发展与投资战略规划报告2022版
  3. 普通话测试第四题评分标准_普通话考试中命题说话的扣分标准
  4. 2012 Microsoft Intern Hiring Written Test [转]
  5. 关于 SAP WebIDE 不支持 ES6 语法的问题
  6. linux板级初始化
  7. java下拉列表 动态_【示例】教你简单用Java写一个动态更新的下拉列表(无数据库)...
  8. 实用UI设计需要学什么软件?
  9. python selenium+pywin32 实现网页另存为
  10. 高项、高级项目管理师论文-整体管理
  11. 元宇宙需要“基建狂魔”
  12. Win10 将 Bookmarks 的书签恢复到 Chrome
  13. 计算机桌面保护时间,请教一个屏幕保护的问题,域内的计算机可以设置不同时间的屏幕保护策略吗?...
  14. 万字拆解Ubras:年销15亿、暴涨10倍,内衣新秀的春天到了?
  15. 忙活了一天,PDF转epub基本成功
  16. video实现控制视频进度和播放、暂停
  17. 共享店铺模式是怎么样的一个模式? 共享店铺系统多少钱一套?
  18. 初一数学绩差,需要补习初一数学吗?
  19. java-net-php-python-SSM的美工接单系统计算机毕业设计程序
  20. HTML+CSS - 侧边导航栏

热门文章

  1. Ehcache(06)监听器
  2. Netstat命令(windows下)
  3. 【caffe】基本数据结构blob
  4. linux基础命令练习1
  5. android volley http请求框架
  6. Linux下配置FTP、SSH服务
  7. asp.net引用用户控件
  8. 【整理】ABAP快捷启动Debug三种方式
  9. 【PP操作手册】工艺路线相关操作
  10. SAP ABAP 客户退出