一、bs4简介
1.1 基本概念
- Beautiful Soup是⼀个可以从HTML或XML⽂件中提取数据的⽹⻚信息提取库。(名称取自爱丽丝梦游仙境)其必需依靠lxml模块!
1.2 源码分析
- github下载源码
- 安装
pip install lxml
pip install bs4
二、bs4的使用
2.1 快速开始
- 经典代码
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
“漂亮打印输出”:
bs4的一些基本使用方法如下图:
如果要查找全部,则用find_all('字符串')
eg.
查找相关数据:
2.2 bs4的对象种类(结合经典代码)
-
tag : 标签
eg.
-
NavigableString : 可导航的字符串
eg.
BeautifulSoup : bs对象
- Comment : 注释