DEV Community

domonic
domonic

Posted on

python3 -m pip install domonic

https://github.com/byteface/domonic/

A DOM for making HTML with python 3! (and more)

Install

python3 -m pip install domonic
# python3 -m pip install domonic --upgrade 
Enter fullscreen mode Exit fullscreen mode

Creating HTML with Python 3

from domonic.html import *
print(html(body(h1('Hello, World!'))))
# <html><body><h1>Hello, World!</h1></body></html>
Enter fullscreen mode Exit fullscreen mode

or to pretty format and insert the doctype, use an f-string:

mydom = html(body(h1('Hello, World!'), a("somelink", _href="somepage.html")))
print(f"{mydom}")
Enter fullscreen mode Exit fullscreen mode
<!DOCTYPE html>
<html>
    <body>
        <h1>Hello, World!</h1>
        <a href="somepage.html">somelink</a>
    </body>
</html>
Enter fullscreen mode Exit fullscreen mode

parsing html

Basic useage...

from domonic import domonic
mydom = domonic.parseString('<somehtml...')
Enter fullscreen mode Exit fullscreen mode

To quickly parse a webapge try the window module...

from domonic.window import window
window.location = "http://www.google.com"
print(window.document.title)
Enter fullscreen mode Exit fullscreen mode

Also try the xpath or css selectors on command line...

domonic -x https://google.com '//a[@class]' | uniq | sort
Enter fullscreen mode Exit fullscreen mode
domonic -q https://google.com 'a' | uniq | sort
Enter fullscreen mode Exit fullscreen mode

https://github.com/byteface/domonic/

Top comments (0)