文章背景:进行网络爬虫时,通过Requests模块获取网页的全部内容,借助BeautifulSoup模块从网页中提取内容。本文对BeautifulSoup模块的使用进行简单的介绍。
例子中用到的HTML页面地址:https://python123.io/ws/demo.html
代码语言:javascript复制<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库是解析、遍历、维护标签树
的功能库。
1 BeautifulSoup库的解析器2 BeautifulSoup类的基本元素3 基于bs4库的HTML内容遍历方法3.1 标签树的下行遍历3.2 标签树的上行遍历3.3 标签树的平行遍历4 bs4库的prettify()方法
1 BeautifulSoup库的解析器
代码语言:javascript复制soup = BeautifulSoup('<html>data</html>','html.parser')
2 BeautifulSoup类的基本元素
代码语言:javascript复制<p class="title"> ... </p>
3 基于bs4库的HTML内容遍历方法
3.1 标签树的下行遍历
代码语言:javascript复制from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.body.contents
代码语言:javascript复制['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']
代码语言:javascript复制len(soup.body.contents)
代码语言:javascript复制5
代码语言:javascript复制# 遍历儿子节点
for child in soup.body.children:
print(child)
代码语言:javascript复制# 遍历子孙节点
for child in soup.body.descendants:
print(child)
3.2 标签树的上行遍历
代码语言:javascript复制from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.title.parent
代码语言:javascript复制<head><title>This is a python demo page</title></head>
代码语言:javascript复制for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)
代码语言:javascript复制p
body
html
[document]
遍历所有先辈节点,包括soup本身。
3.3 标签树的平行遍历
平行遍历发生在同一个父节点下的各个节点之间。
代码语言:javascript复制from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.a.next_sibling
代码语言:javascript复制' and '
代码语言:javascript复制soup.a.previous_sibling
代码语言:javascript复制'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:rn'
代码语言:javascript复制# 遍历后续节点
for sibling in soup.a.next_siblings:
print(sibling)
代码语言:javascript复制# 遍历前续节点
for sibling in soup.a.previous_siblings:
print(sibling)
4 bs4库的prettify()方法
代码语言:javascript复制from bs4 import BeautifulSoup
import requests
r = requests.get("https://python123.io/ws/demo.html")
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
soup.prettify()
代码语言:javascript复制'<html>n <head>n <title>n This is a python demo pagen </title>n </head>n <body>n <p class="title">n <b>n The demo python introduces several python courses.n </b>n </p>n <p class="course">n Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:n <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">n Basic Pythonn </a>n andn <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">n Advanced Pythonn </a>n .n </p>n </body>n</html>'
.prettify()方法为HTML文本<>及其内容增加'n'。
参考资料:
[1] 中国大学MOOC: Python网络爬虫与信息提取(https://www.icourse163.org/course/BIT-1001870001)