使用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 库等,可以更加高效进行网页访问及内容获取。

相关推荐
zhanlang02172 小时前
HTML 零基础入门到企业实战【第七章 7.5】表单验证基础属性:required、maxlength、placeholder
前端·html
kyle~3 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享4 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者4 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.5 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人6 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__6 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
用户0332126663677 小时前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel
LiaoWL1237 小时前
从零搭一个 RAG 知识库(二):换上语义 Embedding——BGE 实测与阈值重校准
python·大模型·numpy·embedding·向量检索·rag
Dmz丶7 小时前
前端转 AI 100 天 Day 48:向量数据库——100 万向量的毫秒级检索
python