DEV Community

domonic
domonic

Posted on

1 1

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/

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay