BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
继上一节。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 BeautifulSoup
soup = 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相关推荐
- 16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
16 bs对象.节点名div.属性contents children descendants 获取子节点 子孙节点 文章目录 16 `bs对象.节点名div.属性contents` children ...
- BeautifulSoup children descendants对比
目录 BeautifulSoup children descendants对比 1 简介 2 验证 2.1 数据说明 2.2 案例说明 2.3 代码验证 2.4结果输出 3 其它 BeautifulS ...
- BeautifulSoup的高级应用 之 find findAll
BeautifulSoup 是python学习的重要组成部分,可用于帮助解析html/XML等内容,尤其是在爬取特定网页信息的时候,用于解析和检查在网上看到的那些乱七八糟而且不规范的HTML页面.至于 ...
- 【javascript高级教程】JavaScript 字符串(String) 对象
String 对象用于处理已有的字符块. JavaScript 字符串 一个字符串用于存储一系列字符就像 "John Doe". 一个字符串可以使用单引号或双引号: var car ...
- Beautiful Soup 4.4.0 文档
Beautiful Soup 4.4.0 文档 文章目录 Beautiful Soup 4.4.0 文档 @[toc] 快速开始 安装 Beautiful Soup 安装完成后的问题 安装解析器 如何 ...
- BeautifulSoup安装及其应用
2019独角兽企业重金招聘Python工程师标准>>> BeautifulSoup 安装及其使用 BeautifulSoup 是个好东东. 官网见这里: http://www.cru ...
- python爬虫05 - BeautifulSoup4的安装,下载,源码简介,使用。
1. bs4简介 1.1 基本概念 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的网页信息提取库 1.2 源码分析 • github下载源码 • 安装 • pip inst ...
- 爬虫 第三讲 数据解析
文章目录 爬虫 第三讲 数据解析 一.正则表达式 1.match()函数.search()函数.findall()函数 2.正则表达式中的元字符 3.正则表达式模式 4.正则表达式重复匹配 5.正则表 ...
- Python爬虫编程4——数据解析模块之bs4
目录 一.bs4简介 1.基本概念 2.源码分析 二.bs4的使用 1.快速开始 2.bs4的对象种类 三.遍历文档树 遍历子节点 1.contents children desc ...
最新文章
- c#_static静态
- [物理学与PDEs]第4章习题3 一维理想反应流体力学方程组的数学结构
- 迷雾世界无限号服务器,迷雾世界部分服务器互通公告_迷雾世界部分服务器3月31日数据互通详情分析_手心游戏...
- DQL——数据查询语言
- java 文件流的帮助类
- Python下安装Opencv
- 最牛ai波士顿动力上台阶_波士顿动力的位置如何使美国成为人工智能的关键参与者...
- 10、Cocos2dx 3.0游戏开发找小三之容器篇:Vector、Map、Value
- 我的世界服务器显示fps,我的世界提升fps的方法 低配玩家必备秘籍
- 个人收藏机器学习教程
- 第三代oid铺码软件_你好点读笔!自制小达人点读目录册之书名贴铺码
- 如何压缩图片呢?这两种方法很管用
- 金盾视频加密器V2014视频加密原理分析
- 中国交通标志检测数据集
- JAVA 命令执行 学习笔记
- PAT 考试是什么?
- 时间序列的特征工程——针对Hurst指数的Python计算
- angularjs pdf插件_AngularJS实战 PDF 下载
- NP 10抗菌肽是什么?有什么作用?
- 屏蔽上网时弹窗广告,防止追踪、恶意域名,过滤横幅广告、以及视频广告的方法
热门文章
- linux内核网络协议栈--br_pass_frame_up和br_forward(二十九)
- leetcode算法题--骑士在棋盘上的概率★
- leetcode算法题--最大整除子集
- android 记一次富文本加载之路
- 阿里云ubuntu14.04下lamp环境搭建の备忘
- python requests post 中文乱码问题
- Linux 最简单的驱动程序hello world
- Java后台解析前台的get中文请求
- 【7】nagios从零学习使用 - nrpe插件使用
- linux下安装jdk+tomcat+eclipse+mysql