使用bs4 分析html文件

首先需要 pip install beautifulsoup4安装

然后为了方便学习此插件,随便打开一个网页,然后鼠标右键,打开源网页,如下图片

这样就可以获得一个网页源码,全选复制粘贴到本地,存储为 .html

文件,后续的学习以此html文件为模版进行

python3 复制代码
from bs4 import BeautifulSoup
import os

# html文件放置的路径和名字
filePath = os.path.join(os.getcwd(), "HTML", "1.html")
print(filePath)
# 打开html文件,注意encoding格式
with open(filePath, "r", encoding="UTF-8") as f:
    html_content = f.read()
# 把这个html进行解析
soup = BeautifulSoup(html_content, 'html.parser')

# 这里是抓第一个 <h1>标签的文本内容
h1_content = soup.find('h1').get_text()
# 这里是抓第一个<p> 标签的文本内容
p_content = soup.find('p').get_text()

print('h1 content:', h1_content)
print('p content:', p_content)
print("--------------------------")
# 这里是抓取所有<p> 标签
p_content_all = soup.find_all('p')
# 利用for 循环进行逐条解析,获取文本内容
for p_content in p_content_all:
    print(p_content.get_text())

如,html文件中含结构

html 复制代码
                  <div class="title_box pd10">
                        <h1>六年前的今天:湖人退役科比的8号和24号球衣</h1>
                        <div class="info_box">
                            <span class="time">2023-12-19</span>
                            <span class="source">直播吧</span>
                        </div>
                    </div>

我使用如下命令:

python3 复制代码
# 使用此命令获取 <h1>标签的文本内容
soup.find('h1').get_text()
# 结果:
六年前的今天:湖人退役科比的8号和24号球衣
soup.find('span', class_='time')
# 结果
2023-12-19

例二:

html内容含结构如下:

html 复制代码
                                                    <div class="disZoom bq_bar">
                                                        <div class="disZoom bar_info">
                                                            <span class="biaoqian">
                                                                 <a href="/?cateid=1005" class="tags">体育</a>
                                                            </span>
                                                            <span class="laiyuan">来源:阿希啥都聊</span>
                                                        </div>
                                                    </div>

使用命令:

python3 复制代码
# 抓取html中出现的第一个以下结构内的内容
soup.find('a', herf="/?cateid=1005")
# 结果是:
体育

类似的结构还有:

html 复制代码
                                    </span>
                                        <p class="tit">早报:华为nova 12价格全曝光 蔚来获22亿美元融资</p>
                                    </a>
python3 复制代码
soup.find('p', class_="tit")

基本上你想要抓取的内容都可以按照格式进行解析获取,是非常方便的

先行记录:

在之后自己构建网页后,自主进行管理,获取,导出网页内容应该都是非常有帮助的,避免反复使用re工具自己分析,太过于繁琐,结合 requests 库等,可以更加高效进行网页访问及内容获取。

相关推荐
tudficdew2 分钟前
实战:用Python分析某电商销售数据
jvm·数据库·python
sjjhd65224 分钟前
Python日志记录(Logging)最佳实践
jvm·数据库·python
2301_821369611 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
机 _ 长1 小时前
YOLO26 改进 | 基于特征蒸馏 | 知识蒸馏 (Response & Feature-based Distillation)
python·深度学习·机器学习
半桔1 小时前
【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典
前端·css·html
_OP_CHEN1 小时前
【前端开发之CSS】(一)初识 CSS:网页化妆术的终极指南,新手也能轻松拿捏页面美化!
前端·css·html·网页开发·样式表·界面美化
2401_832131952 小时前
Python单元测试(unittest)实战指南
jvm·数据库·python
vx_BS813302 小时前
【直接可用源码免费送】计算机毕业设计精选项目03574基于Python的网上商城管理系统设计与实现:Java/PHP/Python/C#小程序、单片机、成品+文档源码支持定制
java·python·课程设计
gzxx2007sddx3 小时前
windows vnpy运行过程及问题记录
python·量化·vnpy
算法_小学生3 小时前
LeetCode 热题 100(分享最简单易懂的Python代码!)
python·算法·leetcode