简单的生成html,简单生成html,[Python]代码cl


[Python]代码

class Tag:    def __init__(self,tag,**attrs):        self.attrs = {}        self.childs= []        self.tag = tag.lower()        if attrs:            self.attr(**attrs)    def __str__(self):        s =  '<'+self.tag;        for a in self.attrs:            s += ' '+a+'=\''+str(self.attrs[a])+'\''        s += '>\n'        for c in self.childs:            s += str(c)        s +='</'+self.tag+'>\n'        return s    def __next__(self):        for c in self.childs:            yield c    def attr(self,**attrs):        for a in attrs:            self.attrs[a] = attrs[a]        return self    def innerhtml(self,*childs):        self.childs = []        self.childs += childs        return self    def append(self,*childs):        self.childs += childs        return self    def __iter__(self):        return iter(self.childs)if __name__ =='__main__':    html = Tag('html').innerhtml(        Tag('head'),\        Tag('body').innerhtml(Tag('a',onmouseover='alert("hello")',href='#',onclick='alert("hello")')\                .innerhtml('hello world')))    print(html)

评论关闭