python beautifulsoup库_python BeautifulSoup库用法总结

6

The Dormouse's story

7

Once upon a time there were three little sisters; and their names were8 ,9 Lacie and10 Tillie;11 and they lived at the bottom of a well.

12

...

13 """

14

15 soup =BeautifulSoup(html)16 soup = BeautifulSoup(open('index.html')) #使用本地文件创建对象

打印一下 soup 对象的内容,格式化输出

1 print soup.prettify()

指定编码:当html为其他类型编码(非utf-8和ascii),比如GB2312的话,则需要指定相应的字符编码,BeautifulSoup才能正确解析。

htmlCharset = "GB2312"soup= BeautifulSoup(respHtml, fromEncoding=htmlChars

from bs4 importBeautifulSoupimportbs4importre#待分析字符串

html_doc = """

The Dormouse's story

The Dormouse's story

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

Elsie,

Lacie

and

Tillie;

and they lived at the bottom of a well.

...

"""

#每一段代码中注释部分即为运行结果#html字符串创建BeautifulSoup对象

soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')#输出第一个 title 标签

print(soup.title)#

The Dormouse's story

#输出第一个 title 标签的标签名称

print(soup.title.name)#title

#输出第一个 title 标签的包含内容

print(soup.title.string)#The Dormouse's story

#输出第一个 title 标签的父标签的标签名称

print(soup.title.parent.name)#head

#输出第一个 p 标签

print(soup.p)"""

The Dormouse's story

"""

#输出第一个 p 标签的 class 属性内容

print(soup.p['class'])#['title', 'aq']

#输出第一个 a 标签的 href 属性内容

print(soup.a['href'])#http://example.com/elsie

'''''

soup的属性可以被添加,删除或修改. 操作方法与字典一样'''

#修改第一个 a 标签的href属性为 http://www.baidu.com/#soup.a['href'] = 'http://www.baidu.com/'

#给第一个 a 标签添加 name 属性#soup.a['name'] = u'百度'

#删除第一个 a 标签的 class 属性为#del soup.a['class']

##输出第一个 p 标签的所有子节点

print(soup.p.contents)"""['\n',

The Dormouse's story

, '\n']"""

#输出第一个 a 标签

print(soup.a)#Elsie#输出所有的 a 标签,以列表形式显示

print(soup.find_all('a'))"""[Elsie,

Lacie,

Tillie]"""

#输出第一个 id 属性等于 link3 的 a 标签

print(soup.find(id="link3"))#Tillie

#获取所有文字内容

print(soup.get_text())"""The Dormouse's story

The Dormouse's story

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

Elsie,

Lacie

and

Tillie;

and they lived at the bottom of a well.

..."""

#输出第一个 a 标签的所有属性信息

print(soup.a.attrs)#{'href': 'http://example.com/elsie', 'class': ['sister'], 'id': 'link1'}

for link in soup.find_all('a'):#获取 link 的 href 属性内容

print(link.get('href'))"""http://example.com/elsie

http://example.com/lacie

http://example.com/tillie"""

#对soup.p的子节点进行循环输出

for child insoup.p.children:print("对soup.p的子节点进行循环输出", child)"""对soup.p的子节点进行循环输出

对soup.p的子节点进行循环输出

The Dormouse's story

对soup.p的子节点进行循环输出"""

#正则匹配,名字中带有b的标签

for tag in soup.find_all(re.compile(r"b")):print(tag.name)"""body

b"""

4. 四大对象种类

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

Tag

NavigableString

BeautifulSoup

Comment

721089-20180326194250480-466938804.png

(1)Tag

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

The Dormouse's story

Elsie

上面的< title>< a> 等、 标签加上里面包括的内容就是 Tag,利用 soup加标签名轻松地获取这些标签的内容,是不是感觉比正则表达式方便多了?不过有一点是,它查找的是在所有内容中的第一个符合要求的标签。soup.title 得到的是title标签,soup.p 得到的是文档中的第一个p标签,要想得到所有标签,得用find_all函数。find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.。

我们可以验证一下这些对象的类型

1 printtype(soup.a)2 #

对于 Tag,它有两个重要的属性,是 name 和 attrs

name

1 printsoup.name2 printsoup.head.name3 #[document]

4 #head

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

attrs

1 printsoup.p.attrs2 #{'class': ['title'], 'name': 'dromouse'}

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

1 print soup.p['class']2 #['title']

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

print soup.p.get('class')#['title']

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

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

2 printsoup.p3 #

The Dormouse's story

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

1 del soup.p['class']2 printsoup.p3 #

The Dormouse's story

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

1 head = soup.find('head')2 #head = soup.head

3 #head = soup.contents[0].contents[0]

4 printhead5

6 html = soup.contents[0] # ...

7 head = html.contents[0] #

...

8 body = html.contents[1] #

...

可以通过Tag.attrs访问,返回字典结构的属性。

或者Tag.name这样访问特定属性值,如果是多值属性则以列表形式返回。

(2)NavigableString

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

1 printsoup.p.string2 #The Dormouse's story

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

1 printtype(soup.p.string)2 #

(3)BeautifulSoup

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

1 printtype(soup.name)2 #

3 printsoup.name4 #[document]

5 printsoup.attrs6 #{} 空字典

(4)Comment

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

我们找一个带注释的标签

1 printsoup.a2 printsoup.a.string3 print type(soup.a.string)

运行结果如下:

1

2 Elsie3

a 标签里的内容实际上是注释,但是如果我们利用 .string 来输出它的内容,我们发现它已经把注释符号去掉了,所以这可能会给我们带来不必要的麻烦。

另外我们打印输出下它的类型,发现它是一个 Comment 类型,所以,我们在使用前最好做一下判断,判断代码如下:

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

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

5. 遍历文档树

(1)直接子节点

Tag.Tag_child1:直接通过下标名称访问子节点。

Tag.contents:以列表形式返回所有子节点。

Tag.children:生成器,可用于循环访问:for child in Tag.children

要点:.contents .children 属性

.contents

tag 的 .content 属性可以将tag的子节点以列表的方式输出。可以使用 [num] 的形式获得。使用contents向后遍历树,使用parent向前遍历树:

1 printsoup.head.contents2 #[

The Dormouse's story]

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

1 printsoup.head.contents[0]2 #

The Dormouse's story

.children

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

可以使用list可以将其转化为列表。当然可以使用for 语句遍历里面的孩子。

1 printsoup.head.children2 #

我们怎样获得里面的内容呢?很简单,遍历一下就好了:

1 for child insoup.body.children:2 print child

结果如下:

1

The Dormouse's story

2

3

Once upon a time there were three little sisters; andtheir names were4 ,5 Lacie and

6 Tillie;7 and they lived at the bottom of a well.

8

9

10

...

(2)所有子孙节点

知识点:.descendants 属性

.descendants

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

Tag.descendants:生成器,可用于循环访问:for des inTag.descendants

1 for child insoup.descendants:2 print child

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

ContractedBlock.gif

ExpandedBlockStart.gif

1

The Dormouse's story

2

3

The Dormouse's story

4

Once upon a time there were three little sisters; andtheir names were5 ,6 Lacie and

7 Tillie;8 and they lived at the bottom of a well.

9

...

10

11

The Dormouse's story

12

The Dormouse's story

13 The Dormouse's story

14

15

16

17

The Dormouse's story

18

Once upon a time there were three little sisters; andtheir names were19 ,20 Lacie and

21 Tillie;22 and they lived at the bottom of a well.

23

...

24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值