Generating HTML

Burak Arslan burak.arslan at arskom.com.tr
Mon Jul 29 08:14:48 EDT 2013


Hi,

On 07/29/13 14:41, Morten Guldager wrote:
> Something like:
>   table_struct = ['table', ['tr', ['td', {class=>"red"}, "this is
> red"],['td', {class=>"blue"}, "this is not red"]]]
>   html = struct2html(table_struct)
>
> Suggestions?
>

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


Python 2.7.5 (default, Jul 16 2013, 00:38:32)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml.html.builder import E
>>> e = E.html(
...     E.head(
...         E.title("Sample Html Page")
...     ),
...     E.body(
...         E.div(
...             E.h1("Hello World"),
...             E.p(
...                 "lxml is quite nice when it comes to generating HTML "
...                 "from scratch"
...             ),
...             id="container",
...         )
...     )
... )
>>> from lxml.html import tostring
>>> print tostring(e, pretty_print=True)
<html>
<head><title>Sample Html Page</title></head>
<body><div id="container">
<h1>Hello World</h1>
<p>lxml is quite nice when it comes to generating HTML from scratch</p>
</div></body>
</html>





More information about the Python-list mailing list