You need to enable JavaScript to run this app. Python如何从HTML文件中提取select元素的id属性值?
优惠活动
大模型
产品
解决方案
定价
更多

Python如何从HTML文件中提取select元素的id属性值?

使用Python提取Flask页面HTML元素的id属性值

你可以借助requests请求页面 + BeautifulSoup解析HTML的方案实现,最终输出id值的列表格式结果。

完整实现代码

场景1:从外部请求Flask页面提取id

import requests
from bs4 import BeautifulSoup

# 替换为你的Flask页面实际访问地址
url = "http://127.0.0.1:5000/你的路由地址"
resp = requests.get(url)
soup = BeautifulSoup(resp.text, "html.parser")

# 收集所有id属性值到列表
id_result = [tag["id"] for tag in soup.find_all(id=True)]
print(id_result)

场景2:在Flask服务端内部直接提取渲染模板的id

from flask import render_template
from bs4 import BeautifulSoup

# 渲染模板得到HTML字符串
html = render_template("你的模板文件.html")
soup = BeautifulSoup(html, "html.parser")

id_result = [tag["id"] for tag in soup.find_all(id=True)]
print(id_result)

输出示例

运行代码后输出格式和你要求的一致,前6个id的输出样例如下:

["tenure_val", "monthly", "total", "gender","Senior", "Partner"]

内容的提问来源于stack exchange,提问作者Talmeez_Coder12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 21:54:01