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

相关推荐
傻啦嘿哟3 小时前
某短视频平台视频爬虫实战:抓取推荐流视频信息,绕过反爬的3种技巧
开发语言·爬虫·python
迷迭香yy4 小时前
集合竞价数据挖掘实战:用Python构建开盘信号识别系统
人工智能·python·数据挖掘
李昊哲小课6 小时前
fastapi sse websocket 奶茶店实时订单看板
人工智能·python·websocket·网络协议·fastapi·sse
2401_844582958 小时前
工具包:软件架构设计的实用技巧与经验分享
python
RFID固定资产管理系统8 小时前
适配媒体行业的固定资产管理软件有哪些功能与核心优势
大数据·人工智能·python·媒体
SunnyDays10119 小时前
Python 为 PowerPoint 添加动画:进入、退出、动作路径与文本动画(详解)
python·powerpoint·动画·动画效果·文本动画
难以怀瑾10 小时前
pytest.param`与 allure.dynamic.title()全解析
python
月光船幽幽11 小时前
Pytest配置实战:高效管理测试标记与覆盖
python·科技
leoZ23112 小时前
实战复盘:用 Claude Code 从零搭一个 GitHub PR 统计工具
java·人工智能·python·深度学习·自然语言处理·github·llama
夏天测12 小时前
Python 网络编程入门:从 Socket 底层到 HTTP 并发实战
python·socket·httpx·requests·tcp udp·python网络编程·爬虫基础