BeautifulSoup文档1-简介、安装和使用
【摘要】 1 BeautifulSoup简介Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库;Beautiful Soup 3 目前已经停止开发,推荐使用Beautiful Soup 4; 2 初步了解注意:以下实例来源于BeautifulSoup官方文档:Beautiful Soup 4.4.0 文档。先看一个文档如下,官网简称为爱丽丝梦游仙境:html_d...
1 BeautifulSoup简介
Beautiful Soup
是一个可以从HTML
或XML
文件中提取数据的Python
库;Beautiful Soup 3
目前已经停止开发,推荐使用Beautiful Soup 4
;
2 初步了解
注意:以下实例来源于BeautifulSoup
官方文档:Beautiful Soup 4.4.0 文档。
- 先看一个文档如下,官网简称为
爱丽丝梦游仙境
:
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>
"""
- 使用
BeautifulSoup
解析上述实例,得到一个BeautifulSoup
的对象,并能按照标准的缩进格式的结构输出:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())
- 输出为:
D:\Python37\python.exe F:/python_study/testother/bs01.py
<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 class="sister" href="http://example.com/elsie" id="link1">
Elsie
</a>
,
<a class="sister" href="http://example.com/lacie" id="link2">
Lacie
</a>
and
<a class="sister" href="http://example.com/tillie" id="link3">
Tillie
</a>
;
and they lived at the bottom of a well.
</p>
<p class="story">
...
</p>
</body>
</html>
进程已结束,退出代码 0
3 BeautifulSoup安装
3.1 Windows系统上安装
pip install beautifulsoup4
3.2 安装解析器
pip install lxml
pip install html5lib
4 BeautifulSoup数据获取几种简单方法
4.1 获取title
print(f"获取title: {soup.title}\n")
- 输出为:
获取title: <title>The Dormouse's story</title>
4.2 获取title.name
print(f"获取title.name: {soup.title.name}\n")
- 输出为:
获取title.name: title
4.3 获取title.string
print(f"获取title.string: {soup.title.string}\n")
- 输出为:
获取title.string: The Dormouse's story
4.4 获取title.parent.name
print(f"获取title.parent.name: {soup.title.parent.name}\n")
- 输出为:
获取title.parent.name: head
4.5 获取第一个p标签
print(f"获取第一个p标签: {soup.p}\n")
- 输出为:
获取第一个p标签: <p class="title"><b>The Dormouse's story</b></p>
4.6 获取p标签中的[‘class’]
print(f"获取p标签中的['class']: {soup.p['class']}\n")
- 输出为:
获取p标签中的['class']: ['title']
4.7 获取第一个a标签
print(f"获取第一个a标签: {soup.a}\n")
- 输出为:
获取第一个a标签: <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
4.8 获取所有a标签
print(f"获取所有a标签: {soup.find_all('a')}\n")
- 输出为:
获取所有a标签:
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
4.9 获取某个指定的链接
print(f"获取某个指定的链接: {soup.find(id='link3')}\n")
- 输出为:
获取某个指定的链接: <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
4.10 获取所有a标签链接
print("获取所有a标签链接:")
for link in soup.find_all('a'):
print(f"{link.get('href')}")
- 输出为:
获取所有a标签链接:
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie
4.11 获取文档中文字内容
print(f"获取文档中文字内容:{soup.get_text()}")
- 输出为:
获取文档中文字内容:
The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...
5 本文涉及的源码
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2023/2/13
# 文件名称:bs01.py
# 作用:BeautifulSoup4的简单使用
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
from bs4 import BeautifulSoup
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>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.prettify())
# 获取title
print(f"获取title: {soup.title}\n")
# 获取title.name
print(f"获取title.name: {soup.title.name}\n")
# 获取title.string
print(f"获取title.string: {soup.title.string}\n")
# 获取title.parent.name
print(f"获取title.parent.name: {soup.title.parent.name}\n")
# 获取第一个p标签
print(f"获取第一个p标签: {soup.p}\n")
# 获取p标签中的['class']
print(f"获取p标签中的['class']: {soup.p['class']}\n")
# 获取第一个a标签
print(f"获取第一个a标签: {soup.a}\n")
# 获取所有a标签
print(f"获取所有a标签: {soup.find_all('a')}\n")
# 获取某个指定的链接
print(f"获取某个指定的链接: {soup.find(id='link3')}\n")
# 获取所有a标签链接
print("获取所有a标签链接:")
for link in soup.find_all('a'):
print(f"{link.get('href')}")
# 获取文档中文字内容
print(f"获取文档中文字内容:{soup.get_text()}")
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)