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.

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

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

  • 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

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"))

CLI

Query a remote page:

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

Query a local file:

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

Pipe HTML in directly:

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

Create a project with a chosen server:

domonic -p myproject --server fastapi

Package Guide

Projects

Indices and Tables