面向对象的python html构建工具包
OOHTML的Python项目详细描述
#oohtml 面向对象的python html构建工具包
简单用法
您可以用两种方法构造html,第一种方法是提供构造函数:
>>> from OOHTML import * >>> C = Constructor() >>> C.feed(o('html'), #open HTML tag o('head'), #open Head c('head'), #close Head o('body'), o('div'), r('Hello World!'), #raw content s('area'), #singleton (self closing element) c('div'), c('body'), c('html'))
或者使用类方法,上面的示例可以重写为:
>>> from OOHTML import * >>> C = Constructor() >>> C.open('html').open('head').close('head').open('body') >>> C.open('div').raw('Hello World!').single('area').close('div') >>> C.close('body').close('html')
要将构造函数呈现为HTML,可以使用render()方法:
>>> C.render()
每个opening、singleton或raw元素都可以有一个id(而不是元素的html id),这样就可以访问它们:
>>> R = r('Hello World', 'message') #a raw element with id 'message' >>> O = o('div', {}, 'menu') #opening element with id 'menu' >>> S = s('img', {}, 'bg') #singleton with id 'bg' >>> C.feed(R) >>> C.getElementById('message') >>> C.removeElementById('message')
可以在另一个构造函数中插入构造函数对象:
>>> sampleConstructor = Constructor() >>> C.addAfterId('sampleID', sampleConstructor)
访问元素和更改其数据的方法有很多:
>>> C.getElementById('message').value #Raw elements use .value to store their data >>> C.getElementById('sample').attributes #Opening and Singleton elements have .attribute >>> C.getElementById('sample').tag #Opening, Closing & Singleton elements have .tag >>> C.getElementById('sample').id #All elements have .id
opening和singleton元素可以具有以下属性:
>>> div = o('div', {'class':['message', 'floater']}, 'contents') >>> C.feed(div) >>> div.addAtributes({'id': ['sample']})
您可以使用此属性选择元素:
>>> C.getElementsByAttributes({'class': ['test']}) #This returns a generator
向您展示一个示例:
>>> from OOHTML import * >>> C = Constructor() >>> C.feed(o('html'), o('head'), o('script', {'type': ['text/javascript'], 'src':['script.js']}), c('script'), o('style', {'type': ['text/css'], 'src':['style.css']}), c('style'), c('head'), o('body'), o('div', {'class':['message']}), r('Hello World!!!', 'message'), s('img', {'alt': ['Hello'], 'src':['img.jpeg']}), c('div'), o('div', {'class':['message', 'floater']}, 'contents'), c('div'), c('body'), c('html')) >>> C.getElementById('message').value = "HTML CONSTRUCTOR!!!" >>> C.addAfterId('contents', Constructor().open('div').raw('OOHTML').single('br').close('div')) >>> print(C.render())
以上输出:
<html> <head> <script type = "text/javascript" src = "script.js"> </script> <style type = "text/css" src = "style.css"> </style> </head> <body> <div class = "message"> HTML CONSTRUCTOR!!! <img src = "img.jpeg" alt = "Hello"/> </div> <div class = "message floater"> <div> OOHTML <br/> </div> </div> </body> </html>
浏览源代码以获取更多信息。
下载
从这里下载软件包:https://pypi.python.org/pypi/OOHTML或使用pip install oohtml安装
项目信息
Github项目页:https://github.com/pooya-eghbali/OOHTML PYPI:https://pypi.python.org/pypi/OOHTML 请发邮件至:persian.writer[网址]gmail.com