用于HTML的Python模块
2 个回答
0
如果你想在不以管理员身份安装Python包的情况下使用它们,可以在你的账户下的某个地方解压virtualenv的源代码,然后直接用Python运行它的virtualenv.py
来创建一个虚拟目录。
$ python ~/virtualenv-1.6.4/virtualenv.py myvenv
$ cd myvenv
$ ls
bin/ include/ lib/
$ . bin/activate
(myvenv)$ pip install whatever...
在这个虚拟环境激活的情况下,你安装的所有包都会放在它的bin
和lib
目录里,而不是系统的目录里,这样这些包只能被myvenv/bin
目录里的python
看到。
我不太喜欢用代码来生成HTML,我更喜欢使用模板,这样一个看起来像HTML的文件可以从我的Python脚本中提取数据,同时仍然“看起来像”HTML。这是一个比较流行的选择,但我手上的机器上没有python2.4
,所以无法测试它是否能在这么老的Python上运行。祝你好运!
1
你试过 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)
输出结果
<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>