前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BeautifulSoup文档1-简介、安装和使用

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

原创
作者头像
虫无涯
发布2023-02-21 09:25:49
4370
发布2023-02-21 09:25:49
举报
文章被收录于专栏:全栈测试技术

1 BeautifulSoup简介

  • Beautiful Soup 是一个可以从HTMLXML文件中提取数据的Python库;
  • Beautiful Soup 3 目前已经停止开发,推荐使用Beautiful Soup 4

2 初步了解

注意:以下实例来源于BeautifulSoup官方文档:Beautiful Soup 4.4.0 文档

  • 先看一个文档如下,官网简称为爱丽丝梦游仙境
代码语言:python
代码运行次数: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 的对象,并能按照标准的缩进格式的结构输出:
代码语言:python
代码运行次数:0
复制
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc, 'html.parser')

print(soup.prettify())
  • 输出为:
代码语言:python
代码运行次数:0
复制
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系统上安装

代码语言:java
复制
 pip install beautifulsoup4

3.2 安装解析器

代码语言:java
复制
pip install lxml

pip install html5lib

4 BeautifulSoup数据获取几种简单方法

4.1 获取title

代码语言:python
代码运行次数:0
复制
print(f"获取title: {soup.title}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取title: <title>The Dormouse's story</title>

4.2 获取title.name

代码语言:python
代码运行次数:0
复制
print(f"获取title.name: {soup.title.name}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取title.name: title

4.3 获取title.string

代码语言:python
代码运行次数:0
复制
print(f"获取title.string: {soup.title.string}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取title.string: The Dormouse's story

4.4 获取title.parent.name

代码语言:python
代码运行次数:0
复制
print(f"获取title.parent.name: {soup.title.parent.name}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取title.parent.name: head

4.5 获取第一个p标签

代码语言:python
代码运行次数:0
复制
print(f"获取第一个p标签: {soup.p}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取第一个p标签: <p class="title"><b>The Dormouse's story</b></p>

4.6 获取p标签中的'class'

代码语言:python
代码运行次数:0
复制
print(f"获取p标签中的['class']: {soup.p['class']}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取p标签中的['class']: ['title']

4.7 获取第一个a标签

代码语言:python
代码运行次数:0
复制
print(f"获取第一个a标签: {soup.a}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取第一个a标签: <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

4.8 获取所有a标签

代码语言:python
代码运行次数:0
复制
print(f"获取所有a标签: {soup.find_all('a')}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取所有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 获取某个指定的链接

代码语言:python
代码运行次数:0
复制
print(f"获取某个指定的链接: {soup.find(id='link3')}\n")
  • 输出为:
代码语言:python
代码运行次数:0
复制
获取某个指定的链接: <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

4.10 获取所有a标签链接

代码语言:java
复制
print("获取所有a标签链接:")
for link in soup.find_all('a'):
    print(f"{link.get('href')}")
  • 输出为:
代码语言:java
复制
获取所有a标签链接:
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie

4.11 获取文档中文字内容

代码语言:java
复制
print(f"获取文档中文字内容:{soup.get_text()}")
  • 输出为:
代码语言:java
复制
获取文档中文字内容:
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 本文涉及的源码

代码语言:java
复制
# -*- 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()}")

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 BeautifulSoup简介
  • 2 初步了解
  • 3 BeautifulSoup安装
    • 3.1 Windows系统上安装
      • 3.2 安装解析器
      • 4 BeautifulSoup数据获取几种简单方法
        • 4.1 获取title
          • 4.2 获取title.name
            • 4.3 获取title.string
              • 4.4 获取title.parent.name
                • 4.5 获取第一个p标签
                  • 4.6 获取p标签中的'class'
                    • 4.7 获取第一个a标签
                      • 4.8 获取所有a标签
                        • 4.9 获取某个指定的链接
                          • 4.10 获取所有a标签链接
                            • 4.11 获取文档中文字内容
                            • 5 本文涉及的源码
                            领券
                            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档