Python ライブラリlxml
は、HTML ドキュメントを生成するための複数のビルダーを提供しているようです。これらの違いは何ですか?
しかし、これらは XHTML ではなくプレーンな HTML を生成します。手動で xmlns 宣言を追加することもできますが、それは洗練されていません。では、lxmlを使用してX HTML ドキュメントを生成するための推奨される方法は何ですか?
lxml.builder.E
http://lxml.de/tutorial.html#the-e-factoryの例:
>>> from lxml.builder import E
>>> def CLASS(*args): # class is a reserved word in Python
... return {"class":' '.join(args)}
>>> html = page = (
... E.html( # create an Element called "html"
... E.head(
... E.title("This is a sample document")
... ),
... E.body(
... E.h1("Hello!", CLASS("title")),
... E.p("This is a paragraph with ", E.b("bold"), " text in it!"),
... E.p("This is another paragraph, with a", "\n ",
... E.a("link", href="http://www.python.org"), "."),
... E.p("Here are some reserved characters: <spam&egg>."),
... etree.XML("<p>And finally an embedded XHTML fragment.</p>"),
... )
... )
... )
lxml.html.builder
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>")
... )
... )