Beautiful Soup库是解析、遍历、维护“标签树”的功能库
Beautiful Soup库,也叫beautifulsoup4 或bs4约定引用方式如下,即主要是用BeautifulSoup类
from bs4 import BeautifulSoup
import bs4
一 Beautiful Soup库解析器
soup = BeautifulSoup('<html>data</html>','html.parser')
解析器 使用方法 条件
bs4的HTML解析器 BeautifulSoup(mk,'html.parser') 安装bs4库
lxml的HTML解析器 BeautifulSoup(mk,'lxml') pip install lxml
lxml的XML解析器 BeautifulSoup(mk,'xml') pip install lxml
html5lib的解析器 BeautifulSoup(mk,'html5lib') pip install html5lib
二 BeautifulSoup类的基本元素
<p class=“title”> … </p>
基本元素 说明
Tag 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾
Name 标签的名字,<p>…</p>的名字是'p',格式:<tag>.name
Attributes 标签的属性,字典形式组织,格式:<tag>.attrs
NavigableString 标签内非属性字符串,<>…</>中字符串,格式:<tag>.string
Comment 标签内字符串的注释部分,一种特殊的Comment类型
2.1 Tag标签:最基本的信息组织单元,分别用<>和</>标明开头和结尾
任何存在于HTML语法中的标签都可以用soup.<tag>访问获得
当HTML文档中存在多个相同<tag>对应内容时,soup.<tag>返回第一个
from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(demo,"html.parser")
>>> soup.title
<title>This is a python demo page</title>
>>> tag = soup.a
>>> tag
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
2.2 Tag的name(名字):标签的名字,<p>…</p>的名字是'p',格式:<tag>.name
每个<tag>都有自己的名字,通过<tag>.name获取,字符串类型
>>> soup.a.name
'a'
>>> soup.a.parent.name
'p'
>>> soup.a.parent.parent.name
'body'
2.3 Tag的attrs(属性):标签的属性,字典形式组织,格式:<tag>.attrs
一个<tag>可以有0或多个属性,字典类型
>>> tag = soup.a
>>> tag.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> tag.attrs['class']
['py1']
>>> tag.attrs['href']
'http://www.icourse163.org/course/BIT-268001'
>>> type(tag.attrs)
<class 'dict'>
>>> type(tag)
<class 'bs4.element.Tag'>
>>>
2.4 Tag的NavigableString:标签内非属性字符串,<>…</>中字符串,格式:<tag>.string
NavigableString可以跨越多个层次
>>> soup.a
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.string
'Basic Python'
>>> soup.p
<p class="title"><b>The demo python introduces several python courses.</b></p>
>>> soup.p.string
'The demo python introduces several python courses.'
>>> type(soup.p.string)
<class 'bs4.element.NavigableString'>
2.5 Tag的Comment:标签内字符串的注释部分,一种特殊的Comment类型
Comment是一种特殊类型
>>> newsoup = BeautifulSoup("<b><!--This is a comment--></b><p>This is not a commment</p>","html.parser")
>>> newsoup.b.string
'This is a comment'
>>> type(newsoup.b.string)
<class 'bs4.element.Comment'>
>>> newsoup.p.string
'This is not a commment'
>>> type(newsoup.p.string)
<class 'bs4.element.NavigableString'>
三 基于bs4库的HTML内容遍历方法
<>…</>构成了所属关系,形成了标签的树形结构
3.1 标签树的下行遍历
属性 说明.contents 子节点的列表,将<tag>所有儿子节点存入列表
.children 子节点的迭代类型,与.contents类似,用于循环遍历儿子节点
.descendants 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历
>>> soup = BeautifulSoup(demo,"html.parser")
>>> soup.head
<head><title>This is a python demo page</title></head>
>>> soup.head.contents
[<title>This is a python demo page</title>]
>>> soup.body.contents
['\n', <p class="title"><b>The demo python introduces several python courses.</b></p>, '\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:
<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>, '\n']
>>> len(soup.body.contents)
5
>>> soup.body.contents[1]
<p class="title"><b>The demo python introduces several python courses.</b></p>
for child in soup.body.children:
print(child) 遍历儿子节点
for child in soup.body.descendants:
print(child) 遍历子孙节点
3.2 标签树的上行遍历 属性 说明
.parent 节点的父亲标签
.parents 节点先辈标签的迭代类型,用于循环遍历先辈节点
>>> soup.title.parent
<head><title>This is a python demo page</title></head>
>>> soup.html.parent
<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>
>>> soup.parent
>>>
>>> soup = BeautifulSoup(demo,"html.parser")
>>> for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)
p
body
html
[document]
遍历所有先辈节点,包括soup本身,所以要区别判断
3.3 标签树的平行遍历
平行遍历发生在同一个父节点下的各节点间
.next_sibling 返回按照HTML文本顺序的下一个平行节点标签
.previous_sibling 返回按照HTML文本顺序的上一个平行节点标签
.next_siblings 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签
.previous_siblings 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签
for sibling in soup.a.next_sibling:
print(sibling) 遍历后续节点
for sibling in soup.a.previous_sibling:
print(sibling) 遍历前续节点
总结:
BeautifulSoup 中文文档BeautifulSoup文档
本篇文章参考https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html