domonic

domonic

A Python DOM that goes way beyond minidom

Domonic is a Python library for generating, parsing, traversing, and manipulating real document trees with the broader web platform in mind.

The method names and mental model deliberately match the browser platform: write div(), query with querySelectorAll(), move nodes with appendChild(), read textContent, and use JavaScript-like helpers such as Array, Date, Promise and URL from Python. The concepts carry across in both directions — for Python developers learning the web platform and for JavaScript developers working in Python.

  • HTML, SVG, DOM, events, CSSOM, geometry, observers, animation, and web APIs

  • A JavaScript-like runtime surface for practical porting and scripting

  • diffDOM-style patch data for minimal server-side DOM updates

  • BeautifulSlop for Beautiful Soup style querying over real domonic nodes

  • CLI tools for querying pages with XPath and CSS selectors

  • dQuery and d3 included as demanding consumers of the DOM, not just extras

The aim is to track the actual platform rather than invent a parallel helper API:

https://pepy.tech/badge/domonic https://img.shields.io/pypi/pyversions/domonic.svg License Badge PyPI Version

Install

python3 -m pip install domonic
python3 -m pip install --upgrade domonic

For the domonic command line tool, install with pipx:

brew install pipx
pipx ensurepath
pipx install domonic
domonic -x https://example.com '//title'

Quick Example

from domonic.html import *

page = html(
    body(
        h1("Hello, World!"),
        a("docs", _href="https://domonic.readthedocs.io/")
    )
)
print(f"{page}")
<!DOCTYPE html>
<html>
    <body>
        <h1>Hello, World!</h1>
        <a href="https://domonic.readthedocs.io/">docs</a>
    </body>
</html>

DOM Example

from domonic.dom import document
from domonic.html import html

root = html()
card = document.createElement("section")
card.setAttribute("class", "card")
root.appendChild(card)

print(root.querySelectorAll(".card"))

Start Here

Use these copy-paste starting points for common Python web, scraping, DOM, and server-side rendering tasks.

Most examples intentionally use web-platform names. If you learn the domonic version, you are learning vocabulary that transfers back to browser HTML, JavaScript, CSS selectors, XPath, and Web APIs.

And if you already know browser JavaScript, the examples are designed to feel approachable because they keep the DOM vocabulary you already use.

Generate HTML with Python:

from domonic.html import a, article, h1, p

page = article(
    h1("domonic"),
    p("Generate HTML with Python objects."),
    a("Read more", _href="/docs", _class="cta"),
)
print(page)

Parse and query HTML:

from domonic import domonic

page = domonic.parseString("<main><a href='/docs'>Docs</a></main>", parser="html.parser")
print(page.querySelector("a").getAttribute("href"))

Use Beautiful Soup style scraping over real DOM nodes:

from domonic.bs4 import BeautifulSlop

soup = BeautifulSlop("<article><a href='/api'>API</a></article>", "html.parser")
for link in soup.find_all("a", href=True):
    print(link.text, link["href"])

Diff two DOM trees:

from domonic.diffdom import DiffDOM
from domonic.html import div, p

old = div(p("one"))
new = div(p("two"))
changes = DiffDOM().diff(old, new)
print(changes)

Use browser Web APIs in Python:

from domonic.webapi.crypto import crypto
from domonic.webapi.encoding import TextEncoder

print(crypto.randomUUID())
print(TextEncoder().encode("hello"))

Guides

The guide section includes task-focused walkthroughs for Scrape HTML, Server-Side HTML, Live DOM Updates, Parser Performance, and Examples Gallery.

CLI

If you primarily want the CLI, install it with pipx so the command is available on your shell path:

brew install pipx
pipx ensurepath
pipx install domonic

Query a remote page:

domonic -x https://example.com '//title'
domonic -q https://example.com 'a.cta' --attr href --first --parser selectolax

Query a local file:

domonic --xpath-file ./page.html '//a' --count
domonic --query-file ./page.html 'a.cta' --text --parser selectolax

Pipe HTML in directly:

curl -s https://example.com | domonic -x '//a' --count
cat page.html | domonic -q 'a.cta' --attr href --parser selectolax

Create a project with a chosen server:

domonic -p myproject --server fastapi

Package Guide

Projects

Indices and Tables