温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python怎么自动生成一个HTML页面

发布时间:2021-03-24 15:40:47 来源:亿速云 阅读:1574 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关使用Python怎么自动生成一个HTML页面,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

方法一:使用 webbrowser

#coding:utf-8
 
import webbrowser
#命名生成的html
GEN_HTML = "test.html" 
#打开文件,准备写入
f = open(GEN_HTML,'w')
 
#准备相关变量
str1 = 'my name is :'
str2 = '--MichaelAn--'
 
# 写入HTML界面中
message = """
<html>
<head></head>
<body>
<p>%s</p>
<p>%s</p>
</body>
</html>
"""%(str1,str2)
 
#写入文件
f.write(message) 
#关闭文件
f.close()
 
#运行完自动在网页中显示
webbrowser.open(GEN_HTML,new = 1) 
'''
webbrowser.open(url, new=0, autoraise=True) 
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
'''

方法二:使用pyh

#coding:utf-8
from pyh import *
# there is a bug "from pyh import *"
page = PyH('My wonderful PyH page')
page.addCSS('myStylesheet1.css', 'myStylesheet2.css')
page.addJS('myJavascript1.js', 'myJavascript2.js')
page << h2('My big title', cl='center')
page << div(cl='myCSSclass1 myCSSclass2', id='myDiv1') << p('I love PyH!', id='myP1')
mydiv2 = page << div(id='myDiv2')
mydiv2 << h3('A smaller title') + p('Followed by a paragraph.')
page << div(id='myDiv3')
page.myDiv3.attributes['cl'] = 'myCSSclass3'
page.myDiv3 << p('Another paragraph')
page.printOut('a.html')

对于大型的 HTML ,使用方法一可以跟灵活的处理HTML的元素变化。在这里推荐使用方法一生成HTML。

下面是生成的html

<html>
<head></head>
<body>
<p>my name is :</p>
<p>--MichaelAn--</p>
</body>
</html>

以上就是使用Python怎么自动生成一个HTML页面,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI