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))

下一篇我们讲解父节点,兄弟节点,回退和前进。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 04:16:41

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.本篇主要解说.parent .parents .next_sibling .previous_sibling .next_siblings .previous_siblings 本篇博客继续使用上篇的html页面内容: html_doc = """ <html> <head>

C#高级编程四十四天-----string和stringbuilder

System.String类 首先string类是静态的,System.String是最常用的字符串操作类,可以帮助开发者完成绝大部分的字符串操作功能,使用方便. 1.比较字符串 比较字符串是指按照字典排序规则,判定两个字符的相对大小.按照点点规则,在一本英文字典中,出现在前面的单词小于出现在后面的单词.在string类中,常用的比较字符串的方法包括Compare,CompareTo,CompareOrdinal以及Equals,下面进行详细的介绍. Compare()方法是string类的静态

Beautiful Soup

Beautiful Soup 4.2.0 文档 - Beautiful Soup 4.2.0 documentation Navigation index Beautiful Soup 4.2.0 documentation ? Beautiful Soup 4.2.0 文档? Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间.

BeautifulSoup的成员结构

>>> dir(soup)['ASCII_SPACES', 'DEFAULT_BUILDER_FEATURES', 'HTML_FORMATTERS', 'ROOT_TAG_NAME', 'XML_FORMATTERS', '__bool__', '__call__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__form

python库使用整理

1. 环境搭建 l  Python安装包:www.python.org l  Microsoft Visual C++ Compiler for Python l  pip(get-pip.py):pip.pypa.io/en/latest/installing.html n  pip install + 安装包          --安装包(.whl,.tar.gz,.zip) n  pip uninstall + 安装包        --卸载包 n  pip show --files +

Mooc爬虫03-BeautifulSoup

1 基本信息 Beautiful Soup是用于处理解析页面信息的 具体的说, Beautiful Soup库是解析, 遍历, 维护"标签树"的功能库 安装方法 pip install beautifulsoup4 最基本的使用 import requests from bs4 import BeautifulSoup r = requests.get("http://www.baidu.com") soup = BeautifulSoup(r.content, '

子标签和后代标签: .children 和 .descendants

昨天看书,没有用enumurate枚举的时候,完全发觉不了他们的区别,倍感困惑. 今天看了其他人写的教程,用了枚举法,然后就发现他们之间的区别了. 还要注意,子代接下一个子代时,可能是换行符,看children找出的子代,你就可以体会到啦! descendants是找了子标签,然后再一步步探入别人的家中,把一个个后台,按辈分由大往小找着. html =""" <html> <head> <title>The Dormouse's story

BeautifulSoup 的用法

转自:http://cuiqingcai.com/1319.html Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快,推荐安装. <thead”> 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(markup, “html.parser”) Python的内置标准库 执行速度适中 文档容错能力强 Python 2

【python】--BeautifulSoup

背景 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序. Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码.你不需要考虑编码方式,除非文档没有指定一个编码方式,这时,Beautif