python自动生成html_使用Python自动生成HTML的方法示例

python 自动化批量生成前端的HTML可以大大减轻工作量

下面演示两种生成 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 = """

%s

%s

"""%(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 << h1('My big title', cl='center')

page << div(cl='myCSSclass1 myCSSclass2', id='myDiv1') << p('I love PyH!', id='myP1')

mydiv2 = page << div(id='myDiv2')

mydiv2 << h2('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

my name is :

--MichaelAn--

这样的方法可以生成简单的HTML,这种使用python自动生成HTML的思想需要有。未来使用人工智能等技术,可以根据已有网站进行深度学习,从而自动绘制新的网站。所以,前端工程化和未来发展需要重视。

您可能感兴趣的文章:Python实现html转换为pdf报告(生成pdf报告)功能示例详解Appium+Python之生成html测试报告Python使用pyh生成HTML文档的方法示例python解析html提取数据,并生成word文档实例解析python使用 HTMLTestRunner.py生成测试报告Python3实现抓取javascript动态生成的html网页功能示例在Python中使用CasperJS获取JS渲染生成的HTML内容的教程python读取html中指定元素生成excle文件示例python将xml xsl文件生成html文件存储示例讲解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值