遍历文档树:

  1、查找子节点

  .contents  

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

  print soup.body.contents

  print type(soup.body.contents)

  运行结果:

[u'\n', <p class="title" name="dromouse"><b>The Dormouse's story</b></p>, u'\n', <p class="story">Once upon a time there were three little sisters; and their names were\n<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,\n<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and\n<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;\nand they lived at the bottom of a well.</p>, u'\n', <p class="story">...</p>, u'\n']

<type 'list'>
[Finished in 0.2s]

.children

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

我们可以打印输出,可以发现它返回的是一个list生成器对象

print soup.body.children

<listiterator object at 0x0294DE90>

我们怎样获得里面的内容呢?遍历一下就ok了:

for child in  soup.boyd.children:

  print child

运行返回内容:

<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 class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>

[Finished in 0.2s]

2、所有子孙节点

.descendants

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

for child in soup.descendants:
  print child

运行结果如下,可以发现,所有的节点都被打印出来了,先生最外层的 HTML标签,其次从 head 标签一个个剥离,以此类推。

3、节点内容

.string

如果一个标签里面没有标签了,那么 .string 就会返回标签里面的内容。如果标签里面只有唯一的一个标签了,那么 .string 也会返回最里面的内容。

果tag包含了多个子节点,tag就无法确定,string 方法应该调用哪个子节点的内容, .string 的输出结果是 None

print soup.head.string
print soup.title.string
print soup.body.string

#The Dormouse's story
#The Dormouse's story
#None
[Finished in 0.2s]

4、多个内容

.strings

获取多个内容,不过需要遍历获取

for string in soup.strings:

  print repr(string)

  .stripped_strings 

  输出的字符串中可能包含了很多空格或空行,使用 .stripped_strings 可以去除多余空白内容

for string in soup.stripped_strings:
  print repr(string)

运行结果:

u"The Dormouse's story"
u"The Dormouse's story"
u'Once upon a time there were three little sisters; and their names were'
u','
u'Lacie'
u'and'
u'Tillie'
u';\nand they lived at the bottom of a well.'
u'...'
[Finished in 0.2s]

5、父节点

 .parent 

print soup.p.parent.name

print soup.head.title.string.parent.name

#body

#title

6、兄弟节点、前后节点等略

 

爬虫库之BeautifulSoup学习(三)的更多相关文章

  1. 爬虫库之BeautifulSoup学习(一)

    Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. 官方解释如下: Beautiful Soup提供一些简单的.pytho ...

  2. 爬虫库之BeautifulSoup学习(五)

    css选择器: 我们在写 CSS 时,标签名不加任何修饰,类名前加点,id名前加 #,在这里我们也可以利用类似的方法来筛选元素,用到的方法是 soup.select(),返回类型是 list 1)通过 ...

  3. 爬虫库之BeautifulSoup学习(四)

    探索文档树: find_all(name,attrs,recursive,text,**kwargs) 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件 1.name参数,可以查找所有 ...

  4. 爬虫库之BeautifulSoup学习(二)

    BeautifulSoup官方介绍文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html 四大对象种类: Beau ...

  5. 【Python】在Pycharm中安装爬虫库requests , BeautifulSoup , lxml 的解决方法

    BeautifulSoup在学习Python过程中可能需要用到一些爬虫库 例如:requests BeautifulSoup和lxml库 前面的两个库,用Pychram都可以通过 File--> ...

  6. 使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解(新手必学)

    为大家介绍下Python爬虫库BeautifulSoup遍历文档树并对标签进行操作的详细方法与函数下面就是使用Python爬虫库BeautifulSoup对文档树进行遍历并对标签进行操作的实例,都是最 ...

  7. python爬虫解析库之Beautifulsoup模块

      一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会 ...

  8. 一起学习PHP中GD库的使用(三)

    上篇文章我们已经学习了一个 GD 库的应用,那就是非常常用的制作验证码的功能.不过在现实的业务开发中,这种简单的验证码已经使用得不多了,大家会制作出更加复杂的验证码来使用.毕竟现在的各种外挂软件已经能 ...

  9. PYTHON 爬虫笔记五:BeautifulSoup库基础用法

    知识点一:BeautifulSoup库详解及其基本使用方法 什么是BeautifulSoup 灵活又方便的网页解析库,处理高效,支持多种解析器.利用它不用编写正则表达式即可方便实现网页信息的提取库. ...

随机推荐

  1. OpenJDK 源码阅读之 LinkedList

    概要 类继承关系 java.lang.Object java.util.AbstractCollection<E> java.util.AbstractList<E> java ...

  2. iOS 移动开发周报

    iOS 移动开发周报   前言 是的,我又开始写周报了!主要是因为喵神不写周报了,加上我发现大家对写 iOS 技术周报这件事情似乎没什么兴趣.其实我觉得这是一个挺好的学习总结的办法,所以要不就继续我来 ...

  3. THE MARTIAN

    影片的最后一段自白 When I was up there, stranded by myself …… “did I think I was going to die?” Yes, absolute ...

  4. 第 2 章 第 9 题 顺序 & 二分搜索效率分析问题

    问题分析 顺序搜索的时间复杂度是O( n ),二分搜索的时间复杂度级别是O( lgn ).但这并不代表二分的时间开销就一定比顺序的小,因为二分搜索有个前提:元素必须要是有序的.如果仅仅为了二分搜索几个 ...

  5. 图像处理中的数学原理具体解释21——PCA实例与图像编码

    欢迎关注我的博客专栏"图像处理中的数学原理具体解释" 全文文件夹请见 图像处理中的数学原理具体解释(总纲) http://blog.csdn.net/baimafujinji/ar ...

  6. HUD 2031: 进制转换

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. 解决Linq.ToDictionary()时的键重复问题

    今天在使用 Linq 的 ToDictionary() 时发生了异常,提示: System.ArgumentException: 已添加了具有相同键 因为以前一直没有遇到有相同键的情况,所以从来没关注 ...

  8. tomcat部署web应用的4种方法以及部署多个应用

    原文: tomcat部署web应用的4种方法 在Tomcat中有四种部署Web应用的方式,简要的概括分别是: (1)利用Tomcat自动部署 (2)利用控制台进行部署 (3)增加自定义的Web部署文件 ...

  9. 2018.11.23-day27 面向对象(大总结)

    1. 2个内置函数 2.反射 3.内置方法 4.面向对象大总结 思维导图地址:https://www.processon.com/view/link/5bfcfd62e4b0ef094cbf04a7  ...

  10. Java面试-Hibernate总结

    1  Hibernate的检索方式 Ø  导航对象图检索(依据已经载入的对象.导航到其它对象. ) Ø  OID检索(依照对象的OID来检索对象. ) Ø  HQL检索(使用面向对象的HQL查询语言. ...