一、Beautiful Soup库安装
window或Linux均可使用命令 pip install beautifulsoup4
二、HTML文档示例
<!--http://python123.io/ws/demo.html-->
<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>
三、BeautifulSoup类基本元素
基本元素 | 说明 |
---|---|
Tag | 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾 |
Name | 标签的名字, <>...</>的名字是'p' ,格式: <tag> . name |
Attributes | 标签的属性,字典形式组织,格式: <tag>. attrs |
NavigableString | 标签内非属性字符串, <...</>中字符串,格式: <tag>. string |
Comment | 标签内字符串的注释部分,一种特殊的Comment类型 |
>>> import requests
>>> from bs4 import BeautifulSoup
>>> r = requests.get("http://python123.io/ws/demo.html")
>>> demo = r.text
>>> soup = BeautifulSoup(demo,"html.parser")
>>> tag = soup.a
>>> tag
<a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">Basic Python</a>
>>> soup.a.name
'a'
>>> soup.a.attrs
{'href': 'http://www.icourse163.org/course/BIT-268001', 'class': ['py1'], 'id': 'link1'}
>>> soup.a.string
'Basic Python'
>>> newsoup = BeautifulSoup("<b><!--this is a comment--></b><p>123</p>","html.parser")
>>> newsoup.b.string
'this is a comment'
>>> type(newsoup.b.string)
<class 'bs4.element.Comment'>
四、标签树的遍历
BeautifulSoup类型是标签树的根节点
1.下行遍历
属性 | 说明 |
---|---|
. contents | 子节点的列表,将<tag>所有儿子节点存入列表 |
. children | 子节点的迭代类型,与. contents类似,用于循环遍历儿子节点 |
.descendants | 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历 |
>>> soup.head.contents
[<title>This is a python demo page</title>]
>>> 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)
2.上行遍历
属性 | 说明 |
---|---|
.parent | 节点的父亲标签 |
.parents | 节点的先辈标签的迭代类型,用于迭代遍历 |
>>> for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)
p
body
html
[document]
3.平行遍历
属性 | 说明 |
---|---|
.next_ sibling | 返回按照HTML文本顺序的下一个平行节点标签 |
.previous_ sibling | 返回按照HTML文本顺序的.上一个平行节点标签 |
.next_ siblings | 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签 |
.previous_ siblings | 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签 |
>>> soup.a.next_sibling
' and '
>>> soup.a.previous_sibling
'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n'
>>> for sibling in soup.a.next_siblings:
print(sibling)
and
<a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">Advanced Python</a>
.
>>> for sibling in soup.a.previous_siblings:
print(sibling)
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:
五、格式化输出
.prettify()方法,可用于标签,tag.prettify()