Beautiful Soup库的基本元素
- 理解:Beautiful Soup库是解析、遍历、维护“标签树”的功能库,对应一个HTML、XML文档的全部内容
- BS是一个HTML/XML的解析器,主要用于解析和提取HTML/XML数据;
- 基于HTML DOM,会载入整个文档,解析整个DOM树,因此时间和内存开销都会大很多,性能低于lxml;
- BS用来解析HTML较简单,API非常人性化,支持CSS选择器、Python标准库中的HTML解析器,也支持lxml的XML解析器;
- 优点为易上手,但匹配效率不如正则、xpath,推荐正则的使用。
- 基本元素:
- Tag标签:最基本的信息组织单元,分别用<>和</>表明开头和结尾;
- Name标签的名字:< p > * < /p >的名字是’p’,格式< tag >.name;
- Attributes标签的属性:字典形式组织,格式:< tag >.attrs;
- NavigableString标签内非属性字符串:<> * </>中字符串,格式:< tag >.string;
- Comment标签内字符串的注释部分,一种特殊的Comment类型;
from bs4 import BeautifulSoup
import requests #抓取页面
r = requests.get('https://python123.io/ws/demo.html')
r
<Response [200]>
demo = r.text#抓取的数据
demo
'<html><head><title>This is a python demo page</title></head>\r\n<body>\r\n<p class="title"><b>The demo python introduces several python courses.</b></p>\r\n<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n<a href="http://www.icourse163.org/course/BIT-268001" class="py1" id="link1">Basic Python</a> and <a href="http://www.icourse163.org/course/BIT-1001870001" class="py2" id="link2">Advanced Python</a>.</p>\r\n</body></html>'
#解析HTML页面
#创建Beautiful Soup对象
soup = BeautifulSoup(demo,'html.parser')
#抓取的页面数据;bs4的解析器
soup
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>
print(soup.prettify)#有层次感的输出解析后的HTML页面
<bound method Tag.prettify of <html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>>
A.标签(用soup.< tag >访问获得):
- 当HTML文档中存在多个相同< tag >对应内容时,soup.< tag >返回第一个
soup.a#访问标签a
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
soup.title
<title>This is a python demo page</title>
B.标签的名字:每个< tag >都有自己的名字,通过soup.< tag >.name获取,字符串类型
soup.a.name
'a'
soup.a.parent.name#返回<a>标签父辈标签名字
'p'
soup.p.parent.name
'body'
C.标签属性,一个< tag >可以有0或多个属性,字典类型,soup.< tag >.attrs
tag = soup.a
print(tag.attrs)
print(tag.attrs['class'])
print(type(tag.attrs))
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
['py1']
<class 'dict'>
D.Attributes:标签内非属性字符串,格式:soup.< tag >.string,NavigableString可以跨越多个层次
print(soup.a.string)
print(type(soup.a.string))
Basic Python
<class 'bs4.element.NavigableString'>
E.NavigableString:标签内字符串的注释部分,Comment是一种特殊类型(有–>)
print(type(soup.p.string))
<class 'bs4.element.NavigableString'>
F.prettify()为HTML文本<>及其内容增加’\n’,更加有层次感的输出
.prettify()可用于标签,方法:< tag >.prettify()
print(soup.prettify())
<html>
<head>
<title>
This is a python demo page
</title>
</head>
<body>
<p class="title">
<b>
The demo python introduces several python courses.
</b>
</p>
<p class="course">
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
Basic Python
</a>
and
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">
Advanced Python
</a>
.
</p>
</body>
</html>
print(soup)#对比直接输出
<html><head><title>This is a python demo page</title></head>
<body>
<p class="title"><b>The demo python introduces several python courses.</b></p>
<p class="course">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a> and <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>.</p>
</body></html>
print(soup.a.prettify())
print('对比无prettify()输出: \n',soup.a)
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">
Basic Python
</a>
对比无prettify()输出:
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
G.BS4库将任何HTML输入都变成utf-8编码
- python 3.x默认支持编码是utf-8,解析无障碍
newsoup = BeautifulSoup('<a>中文</a>','html.parser')
print(newsoup.prettify())
<a>
中文
</a>