介绍
本篇文章给大家分享的是有关使用Python怎么自动生成一个HTML页面,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
方法一:使用webbrowser
#编码:utf - 8 , import 浏览器 #命名生成的html 时间=GEN_HTML “test.html", #打开文件,准备写入=f 开放(GEN_HTML & # 39; w # 39;) , #准备相关变量 时间=str1 & # 39; my name is : & # 39; 时间=str2 & # 39;——米切尔& # 39; , #,写入HTML界面中 时间=message “““ & lt; html> & lt; head> & lt;/head> & lt; body> & lt; p> % s & lt; p> % s & lt;/body> & lt;/html> “““% (str1 str2) , #写入文件 f.write(消息), #关闭文件 f.close () , #运行完自动在网页中显示 webbrowser.open (GEN_HTML new =, 1), & # 39;& # 39;& # 39; webbrowser.open (url,新=0,,autoraise=True), Display url  using 从而,default 浏览器只If new is 0,,, url is opened 拷贝,same browser window If 可能只If new is 1, a new browser window is opened If 可能只If new is 2, a new browser page (“选项卡”),is opened If 可能只If autoraise is 的确,,,window is raised If possible (note that  under many window managers 却;能够will occur regardless of 从而setting of 却;能够变量)。 & # 39;& # 39;& # 39;
方法二:使用pyh
#编码:utf - 8 得到pyh import * #,there is  a bug “得到pyh import *“; 时间=page PyH (& # 39; My wonderful PyH 页面# 39;) page.addCSS (& # 39; myStylesheet1.css& # 39;,, & # 39; myStylesheet2.css& # 39;) page.addJS (& # 39; myJavascript1.js& # 39;,, & # 39; myJavascript2.js& # 39;) page & lt; & lt;, h2 (& # 39; My  big 标题# 39;,,cl=& # 39;中心# 39;) page & lt; & lt;, div (cl=& # 39; myCSSclass1 myCSSclass2& # 39;,, id=& # 39; myDiv1& # 39;), & lt; & lt;, p(& # 39;小姐:love PyH ! & # 39;,, id=& # 39; myP1& # 39;) 时间=mydiv2 page & lt; & lt;, div (id=& # 39; myDiv2& # 39;) mydiv2 & lt; & lt;, h3 (& # 39; A  smaller 标题# 39;),+,p (& # 39; Followed  by A 段# 39;公司) page & lt; & lt;, div (id=& # 39; myDiv3& # 39;) page.myDiv3.attributes [& # 39; cl # 39;],=, & # 39; myCSSclass3& # 39; page.myDiv3 & lt; & lt;, p (& # 39; Another 段# 39;) page.printOut (& # 39; a.html& # 39;)
对于大型的HTML,使用方法一可以跟灵活的处理HTML的元素变化。在这里推荐使用方法一生成HTML。
下面是生成的HTML
& lt; html> & lt; head> & lt;/head> & lt; body> & lt; p> my  name is : & lt;/p> & lt; p>——米切尔& lt;/p> & lt;/body> & lt;/html>
以上就是使用Python怎么自动生成一个HTML页面,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。