The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

"""

创建 beautifulsoup 对象

1

soup = BeautifulSoup(html)

另外,我们还可以用本地 HTML 文件来创建对象,例如

1

soup = BeautifulSoup(open('index.html'))

上面这句代码便是将本地 index.html 文件打开,用它来创建 soup 对象 下面我们来打印一下 soup 对象的内容,格式化输出

1

print soup.prettify()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie

and

Tillie

;

and they lived at the bottom of a well.

...

以上便是输出结果,格式化打印出了它的内容,这个函数经常用到,小伙伴们要记好咯。

5. 四大对象种类

Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种:

Tag

NavigableString

BeautifulSoup

Comment

下面我们进行一一介绍

(1)Tag

Tag 是什么?通俗点讲就是 HTML 中的一个个标签,例如

1

The Dormouse's story

1

Elsie

上面的 title a 等等 HTML 标签加上里面包括的内容就是 Tag,下面我们来感受一下怎样用 Beautiful Soup 来方便地获取 Tags 下面每一段代码中注释部分即为运行结果

1

2

print soup.title

#

The Dormouse's story

1

2

print soup.head

#

The Dormouse's story

1

2

print soup.a

#

1

2

print soup.p

#

The Dormouse's story

我们可以利用 soup加标签名轻松地获取这些标签的内容,是不是感觉比正则表达式方便多了?不过有一点是,它查找的是在所有内容中的第一个符合要求的标签,如果要查询所有的标签,我们在后面进行介绍。 我们可以验证一下这些对象的类型

1

2

print type(soup.a)

#

对于 Tag,它有两个重要的属性,是 name 和 attrs,下面我们分别来感受一下 name

1

2

3

4

print soup.name

print soup.head.name

#[document]

#head

soup 对象本身比较特殊,它的 name 即为 [document],对于其他内部标签,输出的值便为标签本身的名称。 attrs

1

2

print soup.p.attrs

#{'class': ['title'], 'name': 'dromouse'}

在这里,我们把 p 标签的所有属性打印输出了出来,得到的类型是一个字典。 如果我们想要单独获取某个属性,可以这样,例如我们获取它的 class 叫什么

1

2

print soup.p['class']

#['title']

还可以这样,利用get方法,传入属性的名称,二者是等价的

1

2

print soup.p.get('class')

#['title']

我们可以对这些属性和内容等等进行修改,例如

1

2

3

soup.p['class']="newClass"

print soup.p

#

The Dormouse's story

还可以对这个属性进行删除,例如

1

2

3

del soup.p['class']

print soup.p

#

The Dormouse's story

不过,对于修改删除的操作,不是我们的主要用途,在此不做详细介绍了,如果有需要,请查看前面提供的官方文档

(2)NavigableString

既然我们已经得到了标签的内容,那么问题来了,我们要想获取标签内部的文字怎么办呢?很简单,用 .string 即可,例如

1

2

print soup.p.string

#The Dormouse's story

这样我们就轻松获取到了标签里面的内容,想想如果用正则表达式要多麻烦。它的类型是一个 NavigableString,翻译过来叫 可以遍历的字符串,不过我们最好还是称它英文名字吧。 来检查一下它的类型

1

2

print type(soup.p.string)

#

(3)BeautifulSoup

BeautifulSoup 对象表示的是一个文档的全部内容.大部分时候,可以把它当作 Tag 对象,是一个特殊的 Tag,我们可以分别获取它的类型,名称,以及属性来感受一下

1

2

3

4

5

6

print type(soup.name)

#

print soup.name

# [document]

print soup.attrs

#{} 空字典

(4)Comment

Comment 对象是一个特殊类型的 NavigableString 对象,其实输出的内容仍然不包括注释符号,但是如果不好好处理它,可能会对我们的文本处理造成意想不到的麻烦。 我们找一个带注释的标签

1

2

3

print soup.a

print soup.a.string

print type(soup.a.string)

运行结果如下

1

2

3

Elsie

a 标签里的内容实际上是注释,但是如果我们利用 .string 来输出它的内容,我们发现它已经把注释符号去掉了,所以这可能会给我们带来不必要的麻烦。 另外我们打印输出下它的类型,发现它是一个 Comment 类型,所以,我们在使用前最好做一下判断,判断代码如下

1

2

if type(soup.a.string)==bs4.element.Comment:

print soup.a.string

上面的代码中,我们首先判断了它的类型,是否为 Comment 类型,然后再进行其他操作,如打印输出。

6. 遍历文档树

(1)直接子节点

要点:.contents .children 属性

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

1

2

print soup.head.contents

#[

The Dormouse's story]

输出方式为列表,我们可以用列表索引来获取它的某一个元素

1

2

print soup.head.contents[0]

#

The Dormouse's story

.children 它返回的不是一个 list,不过我们可以通过遍历获取所有子节点。 我们打印输出 .children 看一下,可以发现它是一个 list 生成器对象

1

2

print soup.head.children

#

我们怎样获得里面的内容呢?很简单,遍历一下就好了,代码及结果如下

1

2

for child in soup.body.children:

print child

1

2

3

4

5

6

7

8

9

10

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

(2)所有子孙节点

知识点:.descendants 属性

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

1

2

for child in soup.descendants:

print child

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

The Dormouse's story The Dormouse's story

The Dormouse's story

The Dormouse's story

Once upon a time there were three little sisters; and their names were

,

Lacie and

Tillie;

and they lived at the bottom of a well.

...

python利器怎么用-Python爬虫利器二之Beautiful Soup的用法相关推荐

  1. Python 爬虫利器二之 Beautiful Soup 的用法

    上一节我们介绍了正则表达式,它的内容其实还是蛮多的,如果一个正则匹配稍有差池,那可能程序就处在永久的循环之中,而且有的小伙伴们也对写正则表达式的写法用得不熟练,没关系,我们还有一个更强大的工具,叫 B ...

  2. python爬虫select用法_Python爬虫利器二之Beautiful Soup的用法

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

  3. Python爬虫利器二之Beautiful Soup的用法

    如果一个正则匹配稍有差池,那可能程序就处在永久的循环之中,没关系,我们还有一个更强大的工具,叫Beautiful Soup,有了它我们可以很方便地提取出HTML或XML标签中的内容. 1. Beaut ...

  4. python soup attrs_python爬虫(7)--Beautiful Soup的用法

    1.Beautiful Soup简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. Beautiful Soup提供一些简单的.python式的函数用来 ...

  5. Python爬虫入门(8):Beautiful Soup的用法

    Python爬虫入门(1):综述 Python爬虫入门(2):爬虫基础了解 Python爬虫入门(3):Urllib库的基本使用 Python爬虫入门(4):Urllib库的高级用法 Python爬虫 ...

  6. Python网络爬虫:基础知识Beautiful Soup

    一.Beautiful Soup简介 网络数据挖掘指的是从网站中获取数据的过程,数据挖掘技术可以让我们从网站世界中收集大量有价值的数据. Beautiful Soup是一个Python库,可以从HTM ...

  7. JB的Python之旅-爬虫篇--urllib和Beautiful Soup

    啃面包是辛苦的,那就开始学习爬虫吧,而学习爬虫的初衷很简单,爬图爬图,这就是学习的动力~ 1.爬虫信息了解 1)爬虫的定义: 先了解,什么叫爬虫,上度娘搜了一番,解释如下: 网络爬虫(又被称为网页蜘蛛 ...

  8. Python爬虫(三)Beautiful Soup 实战,爬取前程无忧网站

    Beautiful Soup介绍 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能. Beautiful Soup自动将输入文档转换为Unicode ...

  9. python中beautifulsoup_面向新手解析python Beautiful Soup基本用法

    Beautiful Soup就是Python的一个HTML或XML的解析库,可以用它来方便地从网页中提取数据.它有如下三个特点: Beautiful Soup提供一些简单的.Python式的函数来处理 ...

最新文章

  1. Go 知识点(17)— go 工具链 go test 使用
  2. [模拟]纺车的轮子 Spinning Wheels
  3. Python 常用代码片段
  4. 成功解决AttributeError: module 'tensorflow' has no attribute 'scalar_summary'
  5. 【python】动态调用函数名
  6. 计算机原理寄存器基础知识,微机原理——基础知识及计算机基本组成
  7. hibernate一对多映射实现
  8. python2中可以使用print()函数吗_Python3中的 print 函数有哪些用法?
  9. 51NOD 1181 质数中的质数(质数筛法)
  10. Illustrator 教程,如何在 Illustrator 中添加一行文字?
  11. Atitit 人员级别评定法 目录 1.1. 十级评定法1--10 vs 年级评定法 1 1.2. 工龄评定 职级 岗位级别 1 2. 修订系数 学历*授课+绩效 1 3. 计算方法 1
  12. 记录一个好用的压缩软件 HandBrake
  13. 中断系统c语言程序,51单片机中断系统程序实例 (STC89C52RC)
  14. WPF开发之解决程序调用自定义dll库在部分win7电脑上无法运行的情况
  15. 为什么使用用友盟的摇一摇截屏功能,截出来的地图图片是黑屏的,渴求大师解答,感激不尽
  16. HAIKUO-SOFT 作品推荐 四叶草 よつのは 攻略
  17. More Grounded Image Captioning by Distilling Image-Text Matching Model
  18. centos7 80端口被占用导致nginx或httpd无法启用的解决方法
  19. C# 操作通过word模板合并N个word文档
  20. 谐振电路 - 01 介绍

热门文章

  1. java内存分配 常量池详解
  2. mysql修改表和列
  3. HDU 5375 Gray code
  4. (写给像我一样刚离开校园进入公司的小菜鸟)在领域架构下,如何实现简单的展示页面以及增删改查(第二步)...
  5. Jquery的跨域传输数据(JSONP)
  6. Windows XP Professional系统修复的操作方法
  7. python参考手册--第8章
  8. Atlassian In Action - (Atlassian成长之路)
  9. linux下的c socket编程(4)--server端的继续研究
  10. 11.2 计算机保护条例解读