开发者

python module for html

I've seen few people ask this question. But what i'm looking for is more specific. I need a module that works with python 2.4 and it's only for html (not too new like html5) and potentially css. what i'm going to do is to extract info from a text file or from a database a开发者_如何学Cnd write it to an html file. What do you think is best for this task? I'm new to python and i dont have time to play around with different modules so i need to get the "best" one XD I've looked up markup.py and HTMLTags. what do you think?

thank you very much

***I forgot! Could you also show me how to install the module without root access, if it requires one? T.T


Have you tried lxml?

>>> 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>")
...   )
... )

>>> print lxml.html.tostring(html)

Outputs

<html>
  <head>
    <link href="great.css" rel="stylesheet" type="text/css">
    <title>Best Page Ever</title>
  </head>
  <body>
    <h1 class="heading">Top News</h1>
    <p style="font-size: 200%">World News only on this page</p>
    Ah, and here's some more text, by the way.
    <p>... and this is a parsed fragment ...</p>
  </body>
</html>


To use Python packages without installing them as root, unpack the virtualenv source code under your account somewhere and then run its virtualenv.py directly with Python to create a virtual directory.

$ python ~/virtualenv-1.6.4/virtualenv.py myvenv
$ cd myvenv
$ ls
bin/  include/  lib/
$ . bin/activate
(myvenv)$ pip install whatever...

And all the package installation you do while the virtualenv is active goes right into its bin and lib directories instead of the system ones, and the packages can only be seen by the python in the myvenv/bin directory.

I dislike creating HTML with code; I prefer using templates, so that a file that looks like HTML can pull in data provided by my Python script while still “looking like” HTML. Here is one popular option, but I lack python2.4 on any of my machines to test and see if it will work on a Python that ancient. Good luck!

http://jinja.pocoo.org/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜