Building HTML - garyhurtz/html2json.py GitHub Wiki

The Element class can be used to build up a data structure that represents an HTML document. Simply instantiate an Element, passing it the tag, text, and any attributes:

dut = Element(u'p')

which can be rendered to JSON:

dut.render() --> {u'tag': u'p'}

You can add text:

dut = Element(u'p', u'some text')

dut.render() --> {
        u'tag': u'p', u'text': u'some text'
    }

As well as any attributes, such as a class:

dut = Element(u'p', u'some text', {u'class': u'some class'})

dut.render() --> {
        u'tag': u'p', u'text': u'some text', u'attr': {u'class': u'some class'}
    }

You can also build HTML documents by instantiating and appending child Elements:

dut = Element(u'ul', u'some text', {u'class': u'some class'})
dut.child.append(Element(u'li', u'some text', {u'class': u'some class'}))

dut.render() --> {
        u'tag': u'ul', u'text': u'some text', u'attr': {u'class': u'some class'},
        u'child': [
                {u'tag': u'li', u'text': u'some text', u'attr': {u'class': u'some class'}}
            ]
    }