本篇文章給大家分享的是有關(guān)使用Python怎么自動生成一個HTML頁面,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
方法一:使用 webbrowser
#coding:utf-8 import webbrowser #命名生成的html GEN_HTML = "test.html" #打開文件,準備寫入 f = open(GEN_HTML,'w') #準備相關(guān)變量 str1 = 'my name is :' str2 = '--MichaelAn--' # 寫入HTML界面中 message = """%s
%s
"""%(str1,str2) #寫入文件 f.write(message) #關(guān)閉文件 f.close() #運行完自動在網(wǎng)頁中顯示 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
my name is :
--MichaelAn--
以上就是使用Python怎么自動生成一個HTML頁面,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。