BeautifulSoup文档1-简介、安装和使用

简介: BeautifulSoup文档1-简介、安装和使用

1 BeautifulSoup简介

  • Beautiful Soup 是一个可以从HTMLXML文件中提取数据的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()}")
目录
相关文章
|
9月前
|
Unix Linux Shell
不同RTOS中POSIX接口的实现差异
本文探讨了在开发实时应用时使用POSIX API来实现跨平台和可移植性的策略。
151 1
不同RTOS中POSIX接口的实现差异
|
9月前
|
数据采集 Web App开发 安全
Beautiful Soup和Requests
【5月更文挑战第7天】本文介绍了使用Python中的Requests和Beautiful Soup库创建网络爬虫的方法。Requests库简化了HTTP请求,Beautiful Soup则用于解析HTML和XML文档,便于提取信息。首先,文章解释了两个库的作用和安装步骤。接着,通过实例展示了如何提取网页标题和链接,以及如何下载并保存图片。对于动态加载的内容,文章推荐使用Selenium库模拟浏览器行为。此外,还介绍了如何处理登录认证,包括安全输入密码和从外部文件读取凭据。总结来说,本文提供了Python网络爬虫的基础知识和实用技巧。
86 6
|
Android开发
Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑
原文:Android TV开发总结(三)构建一个TV app的焦点控制及遇到的坑 版权声明:我已委托“维权骑士”(rightknights.com)为我的文章进行维权行动.转载务必转载所有,且须注明出处。
3228 0
|
机器学习/深度学习 自然语言处理
【文本分类】《基于提示学习的小样本文本分类方法》
使用P-turning提示学习,进行小样本文本分类。本文值得学习。
222 0
|
城市大脑 人工智能 算法
一网通管 | 互联网+监管
本文介绍了一网通管 | 互联网+监管的方案概述,方案价值及优势以及最佳实践。
一网通管 | 互联网+监管
SPECCPU 极限调优,突破世界第一
优酷链接: SPECCPU 介绍、测试、极限调优
3202 0
|
Android开发
跨程序共享数据——Content Provider 之 ContentResolver基本用法 & 一个读取系统联系人的Demo
本模块共有四篇文章,参考郭神的《第一行代码》,对Content Provider的学习做一个详细的笔记,大家可以一起交流一下: 跨程序共享数据——Content Provider 之 运行时权限解析以及申请的实现(可完美解决java.
1384 0
|
4月前
|
监控 架构师 Java
JVM进阶调优系列(6)一文详解JVM参数与大厂实战调优模板推荐
本文详述了JVM参数的分类及使用方法,包括标准参数、非标准参数和不稳定参数的定义及其应用场景。特别介绍了JVM调优中的关键参数,如堆内存、垃圾回收器和GC日志等配置,并提供了大厂生产环境中常用的调优模板,帮助开发者优化Java应用程序的性能。
|
XML SQL JSON
MyBatis-Plus - JacksonTypeHandler VS FastjsonTypeHandler
MyBatis-Plus - JacksonTypeHandler VS FastjsonTypeHandler
1254 0

热门文章

最新文章