简体   繁体   English

什么是最支持DOM访问的Pythonic XHTML / HTML解析器/生成器/模板模块?

[英]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.

相关问题 什么是Python中最宽容的HTML解析器? - What’s the most forgiving HTML parser in Python? 什么是访问C库的最pythonic方式 - 例如,OpenSSL? - What's the most pythonic way of access C libraries - for example, OpenSSL? 保持生成器的运行总数的最pythonic方法是什么? - What is the most pythonic way for keeping a running total of occurrences from a generator? 执行生成器表达式的最pythonic 方式是什么? - What is the most pythonic way to have a generator expression executed? 确定字节序的最Pythonic方法是什么? - What's the most Pythonic way of determining endianness? 构建此词典的最pythonic方法是什么? - What's the most pythonic way to build this dictionary? 什么是从类似JSON的blob中解析出这个值的最Pythonic方法? - What's the most Pythonic way to parse out this value from a JSON-like blob? 从模块中检索图像的大多数pythonic方式(HTML) - most pythonic way to retrieve images from within a module (in HTML) 使绑定方法像函数一样运行的最pythonic方法是什么? - What is the most pythonic way to make a bound method act like a function? 构造多模块python程序的最pythonic方法是什么? - What is the most pythonic way to structure a multi-module python program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM