本文介绍了用python生成HTML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,什么是生成HTML文档的最优雅的方式。我目前手动将所有标签附加到一个巨大的字符串,并将其写入文件。有没有一种更优雅的方式来做到这一点? 我找到 from yattag import Doc
doc,tag,text = Doc()。tagtext()
$ b带标签('html'):
带标签('body'):
带tag('p',id ='main'):
text('some text')
with tag('a',href ='/ my-url'):
text ('some link')
result = doc.getvalue()
它看起来像HTML,带来额外的好处,您不必关闭标签。
In python, what is the most elegant way to generate HTML documents. I currently manually append all of the tags to a giant string, and write that to a file. Is there a more elegant way of doing this?
解决方案
I find yattag to be the most elegant way of doing this.
from yattag import Doc
doc, tag, text = Doc().tagtext()
with tag('html'):
with tag('body'):
with tag('p', id = 'main'):
text('some text')
with tag('a', href='/my-url'):
text('some link')
result = doc.getvalue()
It reads like html, with the added benefit that you don't have to close tags.
这篇关于用python生成HTML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!