python中beautifulsoup是什么库_Python中的BeautifulSoup库简要总结

一、基本元素

BeautifulSoup库是解析、遍历、维护“标签树”的功能库。

引用

1 from bs4 import BeautifulSoup

1 import bs4

html文档-标签树-BeautifulSoup类

1 from bs4 importBeautifulSoup2 soup1 = BeautifulSoup(“data”,”html.parser”)3 soup2 = BeautifulSoup(open(“D://demo.html”),”html.parser”)

※一个BeautifulSoup对象对应一个HTML/XML文档的全部内容

1 import requests

2 from bs4 importBeautifulSoup3 r = requests.get("http://python123.io/ws/demo.html")4 r.text5 '

This is a python demo page\r\n\r\n

The demo python introduces several python courses.

\r\n

Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\nBasic Python and Advanced Python.

\r\n'

6 demo =r.text7 soup = BeautifulSoup(demo,"html.parser")8 print(soup.prettify())9

10

11

12 This isa python demo page13

14

15

16

17

18 The demo python introduces several python courses.19

20

20

22 Python is a wonderful general-purpose programming language. You can learn Python fromnovice to professional by tracking the following courses:23

24 Basic Python25

26 and

27

28 Advanced Python29

30 .31

32

33

在上述代码中,一个BeautifulSoup对象对应一个HTML文档的全部内容,可查看r,demo,soup的类型

1 type(r)2

3 type(demo)4

5 type(soup)6

BeautifulSoup库解析器

解析器

使用方法

条件

bs4的HTML解析器

BeautifulSoup(mk,’html.parser’)

安装bs4库

lxml的HTML解析器

BeautifulSoup(mk,’lxml’)

安装lxml库

lxml的XML解析器

BeautifulSoup(mk,’xml’)

安装lxml库

html5lib的解析器

BeautifulSoup(mk,’ html5lib’)

安装html5lib库

BeautifulSoup类基本元素

基本元素

说明

Tag

标签,最基本的信息组织单元,分别用<>和>标明开头和结尾

Name

标签的名字,

的名字是’p’,格式:.name

Attributes

标签的属性,字典组织形式,格式:.attrs

NavigableString

标签内非属性字符串,<>…>中字符串,格式:.string

Comment

标签内字符串的注释部分,一种特殊的Comment类型

以"http://python123.io/ws/demo.html"html文档为例,先生成soup对象(BeautifulSoup类),该文档的详细内容在※代码中可看到。

1 import requests

2 from bs4 importBeautifulSoup3 r = requests.get("http://python123.io/ws/demo.html")4 demo =r.text5 soup = BeautifulSoup(demo,"html.parser")

1、soup的标题及标签

文档中有两个a标签,直接使用soup.a只可以获得第一个a标签

1 soup.title #标题

2

This is a python demo page

3 tag = soup.a #标签a

4 tag5 Basic Python

2、标签名

1 soup.a.name #标签a的标签名

2 'a'

3 soup.a.parent.name #标签a的父标签名

4 'p'

5 soup.a.parent.parent.name #标签a的父标签的父标签名

6 'body'

3、标签属性

1 tag = soup.a #tag为soup中的a标签

2 tag.attrs #标签属性值

3 {'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}4 tag.attrs['class'] #标签'class'属性值

5 ['py1']6 tag.attrs['href'] #标签'href'属性值

7 'http://www.icourse163.org/course/BIT-268001'

8 type(tag.attrs) #标签属性类型

9

10 type(tag) #标签类型

11

4、标签内非属性字符串

NavigableString可跨越多个层次,如下代码中p标签内还有b标签,但不会打印b标签,而是打印b标签内的字符串

1 soup.a #标签a

2 Basic Python

3 soup.a.string #标签a内非属性字符串

4 'Basic Python'

5 soup.p #标签p

6

The demo python introduces several python courses.

7 soup.p.string #标签p内非属性字符串

8 'The demo python introduces several python courses.'

9 type(soup.a.string) #标签a内非属性字符串类型

10

11 type(soup.p.string) #标签p内非属性字符串类型

12

5、标签内字符串的注释部分

使用.string时显示的字符串类型可能是NavigableString,也可能是Comment。

1 newsoup = BeautifulSoup("

This is not a comment

","html.parser") #创建一个新的BeautifulSoup对象

2 newsoup.b.string #标签b的字符串

3 'This is a comment'

4 newsoup.p.string #标签p的字符串

5 'This is not a comment'

6 type(newsoup.b.string) #标签b的字符串类型

7

8 type(newsoup.p.string) #标签p的字符串类型

9

综上所述

...

标签 .

名称 .name

属性 .attrs

非属性字符串/注释 .string

二、HTML内容的遍历方法

1、标签树的下行遍历

属性

说明

.contents

子节点的列表,将所有儿子节点存入列表

.children

子节点的迭代类型,与.contents类似,用于循环遍历儿子节点

.descendants

子孙节点的迭代类型,包含所有子孙节点,用于循环遍历

以※处的html文档为例

1 importrequests2 from bs4 importBeautifulSoup3 r = requests.get("http://python123.io/ws/demo.html")4 demo =r.text5 soup = BeautifulSoup(demo,"html.parser")

head标签、head标签儿子节点及body标签儿子节点

1 soup.head #head标签

2

This is a python demo page

3 soup.head.contents #head标签儿子节点

4 [

This is a python demo page]5 soup.body.contents #body标签儿子节点

6 ['\n',

The demo python introduces several python courses.

, '\n',

Python is a wonderful general-purpose programming language. You can learn Python fromnovice to professional by tracking the following courses:7 Basic Python and Advanced Python.

, '\n']8 len(soup.body.contents) #body标签儿子节点数量

9 5

10 soup.body.contents[1] #body标签中的第二个儿子标签

11

The demo python introduces several python courses.

综上

遍历儿子节点

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

遍历子孙节点

for descendant insoup.body.descendantsprint(child)

2、标签树的上行遍历

属性

说明

.parent

节点的父亲标签

.parents

节点先辈标签的迭代类型,用于循环遍历先辈节点

同样的,以※处的html文档为例

1 soup.title.parent #title的父标签

2

This is a python demo page

3 soup.html.parent #html的父标签

4

This is a python demo page

5

6

The demo python introduces several python courses.

7

Python is a wonderful general-purpose programming language. You can learn Python fromnovice to professional by tracking the following courses:8 Basic Python and Advanced Python.

9

10 soup.parent #soup的父标签

由以上代码可知,title标签的父标签为head标签,html标签是html文档的最高级标签,其父标签就是自己。soup是一种特殊的标签,其父标签为空。

标签树的上行遍历

1 soup = BeautifulSoup(demo,"html.parser")2 for parent insoup.a.parents:3 if parent isNone:4 print(parent)5 else:6 print(parent.name)

3、标签树的平行遍历

平行遍历发生在同一个父节点下的各节点间

属性

说明

.next_sibling

返回按照HTML文本顺序的下一个平行节点标签

.previous_sibling

返回按照HTML文本顺序的上一个平行节点标签

.next_siblings

迭代类型,返回按照HTML文本顺序的后续所有平行节点标签

.previous_siblings

迭代类型,返回按照HTML文本顺序的前续所有平行节点标签

同样的,以※处的html文档为例

1 soup.a.next_sibling #标签a的后续平行节点

2 'and'

3 soup.a.next_sibling.next_sibling #标签a后续平行节点的后续平行节点

4 Advanced Python

5 soup.a.previous_sibling #标签a的前续平行节点

6 'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n'

7 soup.a.previous_sibling.previous_sibling #标签a前续平行节点的前续平行节点(为空)

8 soup.a.parent #标签a的父亲节点

9

Python is a wonderful general-purpose programming language. You can learn Python fromnovice to professional by tracking the following courses:10 Basic Python and Advanced Python.

遍历后续节点

1 for sibling insoup.a.next_siblings:2 print(sibling)

遍历前续节点

1 for sibling insoup.a.previous_siblings:2 print(sibling)

三、HTML的格式化和编码

prettify()方法

以※处的html文档为例

1 soup.prettify()2 '\n

\n \n This is a python demo page\n \n \n \n

\n \n The demo python introduces several python courses.\n \n

\n

\n Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n \n Basic Python\n \n and\n \n Advanced Python\n \n .\n

\n \n'

3 print(soup.prettify())4

5

6

7 This isa python demo page8

9

10

11

12

13 The demo python introduces several python courses.14

15

16

17 Python is a wonderful general-purpose programming language. You can learn Python fromnovice to professional by tracking the following courses:18

19 Basic Python20

21 and

22

23 Advanced Python24

25 .26

27

28

编码方式为utf-8

资料来源:《Python网络爬虫与信息提取》——嵩天,北京理工大学,MOOC

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值