为了账号安全,请及时绑定邮箱和手机立即绑定

如何创建一个 HTML 页面,用 Python 中的 n

如何创建一个 HTML 页面,用 Python 中的 n

拉风的咖菲猫 2021-09-24 21:27:37
我想创建一个 HTML 页面,用 n 种不同的颜色说 Hello World n 次,其中 n 是从不同存储库中的配置文件中读取的,并通过 python 程序生成页面,我们可以使用 Javascript 来表示颜色会更有帮助。我可以显示 hello world n 次,但我不知道如何更改颜色。这是我到目前为止编写的代码: import  ConfigParser import webbrowser configParser = ConfigParser.RawConfigParser() configParser.read("/home/suryaveer/check.conf") num = configParser.get('userinput-config', 'num') num2 = int(num) hello = """"hello world """ hello2 = hello*num2 message = """<html><head> </head><body><p>"""+hello2+"""</p></body> </html>""" f = open('x.html', 'w') f.write(message*num2) f.close() webbrowser.open("file:///home/suryaveer/x.html")
查看完整描述

2 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

为了给每个“hello world”设置不同的样式,它们必须位于不同的 HTML 块中。您可以实现将“hello world”的每个实例包装在span.


hello = """"<span>hello world </span>"""

这将不会向您的页面添加任何可见内容,但将允许单独访问每个实例并为其设置样式。


如果你想在 Python 中完成这一切,你可以style在创建它时向 span添加一个内联。


colors = [color1, color2, ... colorN]  # List of n colors


hello2 = ""

for c in colors:

    hello2 += """"<span style='color:{};'>hello world </span>""".format(c)

如果您需要更多帮助或想了解其他选项(Javascript),请告诉我


查看完整回答
反对 回复 2021-09-24
  • 2 回答
  • 0 关注
  • 187 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信