BeautifulSoup文档1-简介、安装和使用
 自在人生  分类:IT技术  人气:67  回帖:0  发布于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()}")
 标签: 暂无标签

讨论这个帖子(0)垃圾回帖将一律封号处理……