Python 的 Markaby/Erector

发布于 2024-10-12 15:22:30 字数 64 浏览 3 评论 0原文

我喜欢使用 Python,但讨厌编写 HTML。 Python 有类似 Markaby/Erector 的模块吗?

I like using Python, but hate writing HTML. Is there a Markaby/Erector - like module for Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍孤女 2024-10-19 15:22:30

似乎你可以用 lxml 做到这一点:

http:// lxml.de/lxmlhtml.html#creating-html-with-the-e-factory

from lxml.html import builder as E
from lxml.html import usedoctest
html = E.HTML(
  E.HEAD(
    E.LINK(rel="stylesheet", href="great.css", type="text/css"),
    E.TITLE("Best Page Ever")
  ),
  E.BODY(
    E.H1(E.CLASS("heading"), "Top News"),
    E.P("World News only on this page", style="font-size: 200%"),
    "Ah, and here's some more text, by the way.",
    lxml.html.fromstring("<p>... and this is a parsed fragment ...</p>")
  )
)

还有Mimsy 看起来很相似。

import makeHTML 
pageTitle = 'Hello World' 
pageHead = makeHTML.part('head') 
pageHead.addPart('title', content=pageTitle) 
pageBody = makeHTML.part('body') 
pageBody.addPart('h1', content=pageTitle) 
pageBody.addPart('p', content="Oh no, not again!") 
pageBody.addPart('hr') 
fullPage = makeHTML.part('html') 
fullPage.addPiece(pageHead) 
fullPage.addPiece(pageBody) 
fullPage.make()

Seems like you can kind of do this with lxml:

http://lxml.de/lxmlhtml.html#creating-html-with-the-e-factory

from lxml.html import builder as E
from lxml.html import usedoctest
html = E.HTML(
  E.HEAD(
    E.LINK(rel="stylesheet", href="great.css", type="text/css"),
    E.TITLE("Best Page Ever")
  ),
  E.BODY(
    E.H1(E.CLASS("heading"), "Top News"),
    E.P("World News only on this page", style="font-size: 200%"),
    "Ah, and here's some more text, by the way.",
    lxml.html.fromstring("<p>... and this is a parsed fragment ...</p>")
  )
)

There is also Mimsy which seems similar.

import makeHTML 
pageTitle = 'Hello World' 
pageHead = makeHTML.part('head') 
pageHead.addPart('title', content=pageTitle) 
pageBody = makeHTML.part('body') 
pageBody.addPart('h1', content=pageTitle) 
pageBody.addPart('p', content="Oh no, not again!") 
pageBody.addPart('hr') 
fullPage = makeHTML.part('html') 
fullPage.addPiece(pageHead) 
fullPage.addPiece(pageBody) 
fullPage.make()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文