遍历文档树:

  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. [Maven实战](9)传递性依赖

    了解Spring的朋友都知道.创建一个Spring Framework项目都须要依赖什么样的Jar包.假设不使用Maven,那么在项目中就须要手动下载相关的依赖.因为Spring Framework又 ...

  2. HMM MEMM CRF 差别 联系

    声明:本文主要是基于网上的材料做了文字编辑,原创部分甚少.參考资料见最后. 隐马尔可夫模型(Hidden Markov Model.HMM),最大熵马尔可夫模型(Maximum Entropy Mar ...

  3. Vue.directive 自定义指令

    一.什么是全局API? 全局API并不在构造器里,而是先声明全局变量或者直接在Vue上定义一些新功能,Vue内置了一些全局API,比如我们今天要学习的指令Vue.directive.说的简单些就是,在 ...

  4. poj3034--Whac-a-Mole(dp)

    题目链接:id=3034">点击打开链接 题目大意:砸地鼠游戏,n*n的方格,锤子每次最多移动d,地鼠在t时刻出如今(x,y)时间.维持一个单位时间,不会在同一时间同一位置出现两仅仅老 ...

  5. UNIX网络编程卷1 时间获取程序client TCP 使用非堵塞connect

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.当在一个非堵塞的 TCP 套接字(可使用 fcntl 把套接字变成非堵塞的)上调用 co ...

  6. 从前端看JavaWeb软件工程中的解耦合

    以下为作者备忘,详情请看 http://www.cnblogs.com/feichengwulai/articles/3412946.html http://blog.csdn.net/piantou ...

  7. EasyIPCamera高性能摄像机RTSP服务器RTSPServer解决方案

    EasyIPCamera EasyIPCamera是由EasyDarwin团队开发的一套非常稳定.易用.支持多种平台(包括Windows/Linux 32&64,Android,ARM his ...

  8. libcurl理解和使用

    1 libcurl是一个很好的客户端库 2 CURLOPT_URL 就是普通的url. 3 CURLOPT_HTTPHEADER 3.1 http get 4 CURLOPT_WRITEFUNCTIO ...

  9. Go Concurrency Patterns: Timing out, moving on

    https://blog.golang.org/go-concurrency-patterns-timing-out-and

  10. JAVA变量初始化赋值null

     在Java中,null值表示引用不指向任何对象.运行过程中系统发现使用了这样一个引用时·可以立即停止进一步的访问,不会给系统带来任何危险. 1.如果是对象的field的话那么系统在初始化对象的时候会 ...