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的更多相关文章
- BeautifulSoup的高级应用 之.parent .parents .next_sibling.previous_sibling.next_siblings.previous_siblings
继上一篇BeautifulSoup的高级应用,主要解说的是contents children descendants string strings stripped_strings.本篇主要解说.pa ...
- BeautifulSoup库children(),descendants()方法的使用
BeautifulSoup库children(),descendants()方法的使用 示例网站:http://www.pythonscraping.com/pages/page3.html 网站内容 ...
- Java高级数据类型转换:包装类、String字符串、Date类等与其他类型转换
1.包装类过渡类型转换 一般情况下,我们首先声明一个变量,然后生成一个对应的包装类,就可以利用包装类的各种方法进行类型转换了.例如: 当希望把float型转换为double型时: float f1=1 ...
- BeautifulSoup的成员结构
>>> dir(soup)['ASCII_SPACES', 'DEFAULT_BUILDER_FEATURES', 'HTML_FORMATTERS', 'ROOT_TAG_NAME ...
- python库使用整理
1. 环境搭建 l Python安装包:www.python.org l Microsoft Visual C++ Compiler for Python l pip(get-pip.py):p ...
- 子标签和后代标签: .children 和 .descendants
昨天看书,没有用enumurate枚举的时候,直接print,完全发觉不了他们的区别,倍感困惑. 今天看了其他人写的教程,用了枚举法,终于,终于,发现它们之间的区别啦!敲锣打鼓,掌声响起来 还要注意, ...
- BeautifulSoup 的用法
转自:http://cuiqingcai.com/1319.html Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python ...
- Python -- BeautifulSoup的学习使用
BeautifulSoup4.3 的使用 下载和安装 # 下载 http://www.crummy.com/software/BeautifulSoup/bs4/download/ # 解压后 使用r ...
- Spider_Man_4 の BeautifulSoup
一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
随机推荐
- Android开发之蓝牙(Bluetooth)操作(二)--修改本机蓝牙设备的可见性,并扫描周围可用的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 修改本机蓝牙设备的可见性 二. 扫描周围可用的蓝牙设备 Eg: 一. 清单文件AdroidManifest.xml: <?xml v ...
- 记录一下 mysql 的查询中like字段的用法
SELECT * from t_yymp_auth_role where role_name not like '%测试%' and role_name not like '%部门%' and rol ...
- php获取csv数据无乱码
<?php //获取csv数据 function csvencode($file){ if(!is_file($file['tmp_name'])){ ...
- Linux学习总结(7)——阿里云centeros服务器上安装 jdk,tomcat,mysql
查看服务器的系统版本 # cat /etc/issue 查看服务器是64位还是32位 #uname -a 或者用:#getconf LONG_BIT 查看当前有没有安装jdk #rpm -q ...
- azure云中 mount: wrong fs type, bad option, bad superblock on /dev/sdc1
2016-01-30 mount失败问题解决 [root@mofficedb2 ~]# mount /dev/sdc /dta mount: you must specify the filesyst ...
- C++中的指针、数组指针与指针数组、函数指针与指针函数
C++中的指针.数组指针与指针数组.函数指针与指针函数 本文从刚開始学习的人的角度,深入浅出地具体解释什么是指针.怎样使用指针.怎样定义指针.怎样定义数组指针和函数指针.并给出相应的实例演示.接着,差 ...
- JAVA学习第五十四课 — IO流(八)打印流 & 序列流
一.综合练习-文件清单列表 获取指定文件夹下,指定扩展名的文件(含子文件夹),并将这些文件的绝对路径写到一个文本文件里.也就是建立一个指定扩展名的文件列表 1.深度遍历 2.过滤器->容器 3. ...
- 使用cecil 完毕 code injection
1. 安装Mono.Cecil watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGFuX2xpYW5n/font/5a6L5L2T/fontsize/400 ...
- jquery12 queue() : 队列方法
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 洛谷P2115 [USACO14MAR]破坏Sabotage
题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...