Skip to main content

Dompy

Dompy turns HTML into an editable Python tree with browser-style DOM methods. It handles malformed markup, contextual fragments, mixed text, HTML templates, and SVG and MathML. Parsing is inert. Dompy preserves scripts and event-handler attributes as data and never executes them.

Install

Dompy requires Python 3.10 or later.

python -m pip install dompy-parser

To install this checkout, run python -m pip install . from the project root.

Parse, query, and edit

from dompy import Document, DompyParser

document = Document.fromString("""
<!doctype html>
<title>Inventory</title>
<main id="inventory">
  <table><tr><td>Apples<td>5</table>
</main>
""")

table = document.querySelector("#inventory > table")
print(document.title)                                  # Inventory
print(table.querySelector("tbody > tr").textContent)    # Apples5

rows = table.getElementsByTagName("tr")
fragment = DompyParser.parseFragment("<tr><td>Pears<td>8", context="tbody")
table.querySelector("tbody").appendChild(fragment)
assert len(rows) == 2

cell = rows[1].querySelector("td")
cell.classList.add("product")
cell.textContent = "Green pears"
assert cell.outerHTML == '<td class="product">Green pears</td>'
  • Document.fromString(html) accepts text or bytes. Byte input supports HTML charset sniffing and an optional encoding.
  • Document.fromFile(path_or_stream) accepts paths and open binary or text streams. Document.fromURL(url, timeout=30) loads an HTTP(S) response and uses its declared charset when present. Document.fromURI(uri) also accepts local file URIs.
  • Document.documentElement (also root), head, body, doctype, title, and compatMode describe the parsed document. Use str(document) to serialize it.
  • querySelector and querySelectorAll accept CSS selectors, including combinators, attribute tests, :scope, :is(), and :has(). Queries return the first element or None, or a snapshot list in document order. Invalid or unsupported selectors raise SelectorError.
  • getElementById matches exact IDs. getElementsByTagName, getElementsByClassName, children, and childNodes provide live sequences. children contains only elements. childNodes includes text and comments.
  • Read markup with innerHTML and outerHTML, and read or write text with textContent. Assigning innerHTML parses markup in the target element's context. HTML template contents live in template.content, a separate DocumentFragment.
  • Create and edit nodes with createElement, createTextNode, createComment, createDocumentFragment, appendChild, insertBefore, replaceChild, removeChild, and cloneNode. Elements also support matches, closest, and attribute and class operations.

DompyParser.parse(html) returns the root Element. The original getElementsByTag and getElementsByClass methods return Python lists. Element.tag exposes the local tag name, and Element.content returns innerHTML for regular elements.

Dompy implements a static HTML DOM. Layout, JavaScript execution, event dispatch, and resource loading from markup are outside its scope. scripting=True changes how the parser handles <noscript>. CSS matching uses Soup Sieve, with limited support for browser state and no pseudo-elements. Serialization preserves potentially active markup, so treat the output as unsanitized HTML.

Test

python -m pip install -e '.[test]'
python -m pytest --cov=dompy

The browser suite compares Python trees, serialization, CSS queries, and fragment edits with Chromium's DOMParser. It runs offline once Chromium is installed.

python -m pip install -e '.[test,browser]'
python -m playwright install chromium
DOMPY_BROWSER=chromium python -m pytest

Set DOMPY_BROWSER=chrome to use a local Google Chrome installation.

License

Dompy is licensed under the GNU GPL v3. See LICENSE.

Metadata

Release files for dompy-parser 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dompy-parser 0.2.0
File Size Uploaded Provenance Actions
dompy_parser-0.2.0.tar.gz 39.8 kB Without provenance Details for dompy_parser-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dompy-parser 0.2.0
File Interpreter ABI Platform Actions
dompy_parser-0.2.0-py3-none-any.whl Python 3 none any Details for dompy_parser-0.2.0-py3-none-any.whl

Total release size: 68.1 kB

Release files / dompy_parser-0.2.0.tar.gz

Download URL dompy_parser-0.2.0.tar.gz
Size 39.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7c32ef89fc68ee16225dbb0497b1264deee37fde6a862619a22acefa034734e5
BLAKE2b-256 checksum
How to use checksums
d020f8729667aa03299c292f8145fdcb3c8124db99a054b4fac0d9e4fe126a21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / dompy_parser-0.2.0-py3-none-any.whl

Download URL dompy_parser-0.2.0-py3-none-any.whl
Size 28.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99f16d0d45e508328c2cb6169f622bf20c35d74f84c3cbae651300e1fd084b46
BLAKE2b-256 checksum
How to use checksums
935c2594e34c66392b6e183c7158e35e3bf0e033a854ac2d72c0e022b2a00124
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for dompy-parser 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page