python - 我如何从 python 导入一个 html 文件
问题描述
我想在 python 中导入一个 html 文件,以便在 python 代码中不写 html
我尝试读取 html 文件并将其内容放入变量中,但它不读取 css 内容
html = open("templates/email.html","r")
msg.add_alternative(html.read(),subtype="html")
html.close()
例子:
import 'file.html' as html
msg.add_alternative(html,subtype="html")
解决方案
这将打开并读取将其放入列表变量的文件。
fileName = "filepath of the html I want to read"
htmlText = file.text.readlines(fileName)
如果您想在 htmlText 变量中(在 HTML 中)找到某个元素。我会建议一个 for 循环和一个子字符串查找方法,如下所示。
for i in range(0, len(htmlText):
line=htmlText[i]
if (line.find('some element inside HTML') != -1):
print("I found it")
# some other action
推荐阅读
- spring-mvc - 当我尝试重定向到任何其他页面时,它会转到 http://localhost:5050/login 而不是 http://localhost:5050/todo/login
- google-colaboratory - 如何通过 Google Colab 文件浏览器查看目录中超过 999 个文件?
- cmake - 如何在不使用 cmake 的 add_library() 的情况下将 lib.a 静态库添加到我的项目中?
- php - How to merge all property values of two objects in PHP
- database - 如何将查询结果插入到 AS400 中的临时表或变量中?
- reactjs - 功能组件中的去抖动
- pytorch - 变换参数作为学习目标
- prometheus - 将 Prometheus 图表从 Prometheus-operator 升级到 kube-Prometheus-stack
- python - 在 python 中使用 selenium 滚动页面,只给我底部元素
- spring - 如何将带有对象的对象发送到服务器Vue.js 3