BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
来源:互联网 发布:宁夏网络安全教育平台 编辑:程序博客网 时间:2024/06/05 23:59
继上一节,BeautifulSoup的高级应用 之 find findAll,这一节,主要讲解BeautifulSoup有关的其他几个重要应用函数。
本篇中,所使用的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>"""
.contents和.children
tag的 .contents 属性可以将 tag的子节点以列表的形式输出。
from bs4 import BeautifulSoupsoup = BeautifulSoup(html_doc)head_tag = soup.head head_tag # <head><title>The Dormouse's story</title></head> head_tag.contents [<title>The Dormouse's story</title>] title_tag = head_tag.contents[0] title_tag # <title>The Dormouse's story</title> title_tag.contents # [u'The Dormouse's story']
BeautifulSoup对象本身一定会包含子节点 ,也就是说 标签也是 BeautifulSoup 对象的子节点 :
len(soup.contents) # 1 soup.contents[0].name # u'html'
字符串没有.contents 属性 ,因为字符串没有子节点 :
text = title_tag.contents[0] text.contents # AttributeError: 'NavigableString' object has no attribute 'contents'
通过 tagtagtag的 .children 生成器 ,可以对 tagtagtag的子节点进行循环 :
for child in title_tag.children: print(child)# The Dormouse's story
.descendants:
.contents和 .children 属性仅包含 tagtagtag的直接子节点 .例如 ,标签只有一个直接子节点
head_tag.contents # [<title>The Dormouse's story</title>]
但是 标签也包含一个子节点 :字符串 字符串 “The Dormouse’s story”, 这种情况下字符串 “The Dormouse’s story” 也属于 标签的子孙节点 .descendants 属性可以对所有 tagtagtag的子孙节 点进行递归循环
for child in head_tag.descendants: print(child) # <title>The Dormouse's story</title># The Dormouse's story
标签只有一个子节点 ,但是有 2个子孙节点 :节点和 的子 节点 , BeautifulSoup 有一个直接子节点 (节点 ), 却有很多子孙节点 :
len(list(soup.children)) #这里是html的children子节点# 1 len(list(soup.descendants)) #这里是html的descendants子孙节点 多个# 25
.string:
如果 tagtagtag只有一个 NavigableString 类型子节点 ,那么这个 tag可以使用.string 得到子节点 :
title_tag.string # u'The Dormouse's story'
如果一个 tag仅有一个子节点 ,那么这个 tag也可以使用 .string 方法 ,输出结果与当前唯一子 节点的 .string 结果相同 .
如果 tag包含了多个子节点,tag就无法确定 .string.string.string .string.方法应该调用哪个子节点的内 , .string 的输出结果是 None。
strings 和 stripped_strings:
如果 tag中包含多个字符串 ,可以使用.strings 来循环获取 :
for string in soup.strings: print(repr(string))
输出的字符串中 可能包含了很多空格或行 ,使用 .stripped_strings 可以去除多余空白内容 :
for string in soup.stripped_strings: print(repr(string))
下一篇我们讲解父节点,兄弟节点,回退和前进。
- BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
- BeautifulSoup库之find、findAll和children、descendants
- BeautifulSoup的高级应用 之 find findAll
- BeautifulSoup的高级应用 之 find findAll
- jquery中find,children,contents的区别
- BeautifulSoup的高级应用 之.parent .parents .next_sibling.previous_sibling.next_siblings.previous_siblings
- BeautifulSoup高级应用 之 CSS selectors /CSS 选择器
- BeautifulSoup高级应用 之 CSS selectors /CSS 选择器
- MDX函数之 - Descendants
- Python爬虫包 BeautifulSoup 学习(七) children等应用
- Linq to XML 之XElement的Descendants方法的新发现
- BeautifulSoup的简单应用
- iOS动画系列之二--CALayer的contents属性的应用
- Strings扩充String的功能
- python3 beautifulsoup 检测是否存在children 元素
- SWT高级控件之SWT的高级应用
- SWT高级控件之SWT的高级应用
- php开发之session的高级应用
- jquery easyui 数据表格datagrid 如何使右侧的 空白去掉
- 搜索引擎(lucene)
- 证书相关,包括APP ID 分发证书 描述文件
- 从CCObject看Cocos2d-x的拷贝机制
- Windows 安装Qt+VS2010/MinGW
- BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
- HDOJ 5399 Too Simple
- Oracle共享池
- 257 Binary Tree Paths
- logback logback.xml 常用配置详解
- php学习笔记1
- 1006--UML类图几种关系的总结
- 九度oj 1089
- PHP学习,数据库操作