[英]What's the most Pythonic XHTML/HTML parser/generator/template module that supports DOM like access?
It should be able to create , modify and read X/HTML in a highly object oriented way that still feels DOM like but is not obese, and is really Pythonic . 它应该能够以高度面向对象的方式创建 , 修改和读取 X / HTML,它仍然感觉DOM喜欢但不肥胖,并且真的是Pythonic 。 Preferably it would deal with malformed HTML too, but we can skip this for templates.
最好它也会处理格式错误的HTML,但我们可以跳过这个模板。
For example, I'd like to do this: 例如,我想这样做:
>> from someAmazingTemplate import *
>> html = Template('<html><head><title>Hi</title></head><body></body></html>')
>> html.head.append('<link type="text/css" href="main.css" rel="stylesheet" />')
>> html.head.title
Hi
>> html['head']['title']
Hi
I should be able to use/define short functions and use them like this: 我应该能够使用/定义短函数并像这样使用它们:
>> html.head.append(stylesheet(href="main.css"))
>> html.body.append(h1('BIG TITLE!12',Class="roflol"))
>> html.body.SOURCE
<body>
<h1 class="roflol">
BIG TITLE!12
</h1>
</body>
Note: If it doesn't exist, I'm going to make it under BSD/MIT/Python license. 注意:如果它不存在,我将使用BSD / MIT / Python许可证。 Help is most welcome.
非常欢迎帮助。 Anything that works towards more Pythonic web app development will be great.
任何有助于更多Pythonic Web应用程序开发的东西都会很棒。 Very much appreciate it!
非常感谢!
-Luke Stanley -Luke Stanley
The first part can for the most part be done by ElementTree , but it takes a few more steps: 第一部分可以在很大程度上由ElementTree完成,但它需要更多的步骤:
>>> import xml.etree.ElementTree as ET
>>> html = ET.XML('<html><head><title>Hi</title></head><body></body></html>')
>>> html.head = html.find('head')
>>> html.head.append(ET.XML('<link type="text/css" href="main.css" rel="stylesheet" />'))
>>> html.head.title = html.head.find('title')
>>> html.head.title.text
'Hi'
The second part can be completed by creating Element objects, but you'd need to do some of your own work to make it happen the way you really want: 第二部分可以通过创建Element对象来完成,但是你需要做一些自己的工作来使它按照你真正想要的方式发生:
>>> html.body = html.find('body')
>>> my_h1 = ET.Element('h1', {'class': 'roflol'})
>>> my_h1.text = 'BIG TITLE!12'
>>> html.body.append(my_h1)
>>> html.body.SOURCE = ET.tostring(html.body)
>>> html.body.SOURCE
'<body><h1 class="roflol">BIG TITLE!12</h1></body>'
You could create a stylesheet
function of your own: 您可以创建自己的
stylesheet
函数:
>>> def stylesheet(href='', type='text/css', rel='stylesheet', **kwargs):
... elem = ET.Element('link', href=href, type=type, rel=rel)
... return elem
...
>>> html.head.append(stylesheet(href="main.css"))
And the whole document: 整个文件:
>>> ET.tostring(html)
<html><head><title>Hi</title><link href="main.css" rel="stylesheet" type="text/css" /></head><body><h1 class="roflol">BIG TITLE!12</h1></body></html>
But, I think if you're going to end up writing your own thing, this is a good place to start. 但是,我认为如果你最终要编写自己的东西,这是一个很好的起点。 ElementTree is very powerful.
ElementTree非常强大。
Edit: I realize that this is probably not exactly what you're looking for. 编辑:我意识到这可能不是你想要的。 I just wanted to provide something as an available alternative and to also prove that it could actually be done without too much work.
我只想提供一些可用的替代方案,并证明它实际上可以在没有太多工作的情况下完成。
Amara Bindery provides the most Pythonic XML API I've seen. Amara Bindery提供了我见过的最多Pythonic XML API。 See the quick reference , manual and faq
请参阅快速参考 , 手册和常见问题解答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.