Skip to main content

dom-tree-rs for Python

Parse messy real-world HTML, query it with CSS selectors, and get readable text or Markdown back — fast, typed, and it never chokes on bad markup. Python bindings for the dom-tree-rs Rust crate.

pip install dom-tree-rs
import domtree

doc = domtree.parse("<ul><li class='x'>a</li><li>b</li></ul>")
[li.text for li in doc.select("li")]   # ['a', 'b']
doc.select_first("li.x").html          # '<li class="x">a</li>'

The distribution is called dom-tree-rs (the name domtree was taken on PyPI); the import name is domtree. Wheels are abi3: one wheel per platform works on every CPython ≥ 3.9 (Linux x86_64/aarch64, macOS Intel/Apple Silicon, Windows).

Scraping

import domtree
import urllib.request

html = urllib.request.urlopen("https://example.com/shop").read().decode()
doc = domtree.parse(html)

products = [
    {
        "name": card.select_first(".title").text,
        "price": card.select_first(".price").text,
        "url": card.select_first("a.buy").attr("href"),
    }
    for card in doc.select("div.card")
]
# [{'name': 'Widget', 'price': '$9.99', 'url': '/buy/1'}, ...]

# Everything a selector engine should do, without a browser:
doc.select("ul > li:nth-child(odd)")
doc.select_first(".card:not(.featured) a[href^='/buy']")
doc.select_first("div:has(> h2:contains('Gadget'))")

# Navigate from any element.
price = doc.select_first("p.price")
price.parent.attrs          # {'class': 'card featured', 'data-id': '1'}
price.closest(".card")      # nearest matching ancestor (or itself)
price.parent.children       # child elements

HTML → Markdown for LLMs and RAG

Feeding raw HTML to a model wastes tokens on markup, scripts and navigation. Pick the content you want and convert it to clean Markdown (CommonMark + GFM: headings, lists, links, images, code blocks, tables, quotes):

doc = domtree.parse(html)

article = doc.select_first("article") or doc.root
markdown = article.to_markdown()
# # Release notes
#
# Version **2.0** is out. See the [docs](/docs).
#
# - Faster
# - Smaller
#
# | OS | Status |
# | --- | --- |
# | Linux | ok |

prompt = f"Summarize this page:\n\n{markdown}"

<script>, <style> and <head> never leak into the output. For plain text instead (a line per block, a blank line between paragraphs) use .text / to_text().

API

domtree.parse(html: str) -> Document

Never raises on malformed HTML — the parser recovers like a browser would (implicit </p>/</li>, stray and mis-nested end tags, …) and records what it recovered from in Document.errors.

Document
select(selector) -> list[Element] every match, in document order
select_first(selector) -> Element | None the first match
to_text() / to_markdown() / to_html() the whole document as text, Markdown or HTML
to_json() -> str the tree as JSON (json.loads it)
errors -> list[tuple[int, str]] (byte_offset, message) recovery diagnostics
root -> Element | None, roots -> list[Element] the first / all top-level elements
Element
tag lower-cased tag name
attrs -> dict[str, str], attr(name, default=None) attributes (valueless ones are "")
text readable text (like to_text())
text_content raw concatenated text (DOM textContent)
html, inner_html outer / inner HTML
select(sel), select_first(sel) matching descendants
closest(sel), matches(sel) nearest matching ancestor-or-self / does it match
parent, children, document navigation
to_markdown(), to_json() this subtree as Markdown / JSON

An invalid or unsupported selector raises ValueError with the reason and position (invalid selector 'a:hover': unsupported pseudo-class ':hover' at byte 1). Supported: type, .class, #id, *, attribute selectors ([a], =, ~=, |=, ^=, $=, *=, i flag), all four combinators, :nth-child()/:nth-of-type() and friends, :first-child, :only-child, :empty, :root, :scope, :not(), :is(), :where(), :has(), :contains(), :checked, :disabled, … — see the main README for the full list.

Elements keep their document alive, compare equal when they are the same node, and are hashable. Documents and elements are immutable, so they are safe to share between threads; large inputs are parsed and serialized with the GIL released, so a thread pool parses pages in parallel.

The package ships type stubs (py.typed), so editors and type checkers know every signature.

Development

cd python
python -m venv .venv && . .venv/bin/activate
pip install maturin pytest
maturin develop --release
pytest

License

MIT

Metadata

Release files for dom-tree-rs 0.3.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 dom-tree-rs 0.3.0
File Size Uploaded Provenance
dom_tree_rs-0.3.0.tar.gz 463.8 kB With provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for dom-tree-rs 0.3.0
File
dom_tree_rs-0.3.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
dom_tree_rs-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
dom_tree_rs-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
dom_tree_rs-0.3.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
dom_tree_rs-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 1.9 MB

Release files / dom_tree_rs-0.3.0.tar.gz

Download URL dom_tree_rs-0.3.0.tar.gz
Size 463.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e5dcf4739810f3f019fc40f0b6eb6df1ef854f9b1606217189d12e83e220066e
BLAKE2b-256 checksum
How to use checksums
d19fafbe45c75da4aff1d5089722505ef2c4b0d792f661ba028262a8fc2c0e53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / dom_tree_rs-0.3.0-cp39-abi3-win_amd64.whl

Download URL dom_tree_rs-0.3.0-cp39-abi3-win_amd64.whl
Size 229.1 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
59564e560a095bd5263b9bf1d73bf9ad43b60e6c4c2a4d2f63c83f8d37b27c93
BLAKE2b-256 checksum
How to use checksums
b3d37a5a3710d20b8749310e9666f9fddd3bc7ac32c89c1bc70073d38cf489b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / dom_tree_rs-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL dom_tree_rs-0.3.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 311.0 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
160b84fe8202554034283b80a9baaf8d6b144df194885a3678478fbb5758656d
BLAKE2b-256 checksum
How to use checksums
d291c3dc53d42a3a74952f63b6b8eb07514ef66203f1c22f18405b853119f39e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / dom_tree_rs-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL dom_tree_rs-0.3.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 297.7 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
32ab371398de2f02b15219e342609733a78efddf1dd4e8ef9daf3916144cc252
BLAKE2b-256 checksum
How to use checksums
79f52b1c90f46411f0b5a095111b8cd1d3255f278951c830aeb049c7b2df8e6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / dom_tree_rs-0.3.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL dom_tree_rs-0.3.0-cp39-abi3-macosx_11_0_arm64.whl
Size 289.7 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c6ad14f90380965b1c74208828e2c6155b97acdffc442f10ef765e06cb69eb33
BLAKE2b-256 checksum
How to use checksums
1af432f6b2a08e986effa1b224a627648bce175926989300112501a78ff81c4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / dom_tree_rs-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL dom_tree_rs-0.3.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 300.3 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
3e8c4aae184d91342e017853f9bbecd6ad2be642384ecc4bc1def7296110fd07
BLAKE2b-256 checksum
How to use checksums
db22fffcb2146d9a6a74f25bc872336ccf688dba0ff6d18bda9c1fd4fa1bbaf2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for dom-tree-rs 0.3.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.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for dom_tree_rs-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

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.3.0 This release

6 release files Full 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