Introduction
Aspose.HTML FOSS for Python is a new open-source library for working with HTML documents in Python. It parses markup into a standards-based document object model, lets you build and modify element trees programmatically, and resolves CSS — including specificity, inline declarations, and inheritance — into computed styles you can read back from any element.
The library targets backend and tooling scenarios where a browser is unavailable or unwanted: services that clean up stored HTML, pipelines that restructure page fragments, and test suites that assert on generated markup. It installs from PyPI as aspose-html-foss, is MIT licensed, and is implemented in pure Python for Python 3.10 and later, so it behaves the same on developer machines, CI runners, and servers.
Beyond the DOM and CSS core, the package ships practical building blocks that HTML processing usually ends up needing anyway: an HTML tokenizer and tree builder you can drive directly, WHATWG-style URL and URLSearchParams classes, and a byte-stream encoding detector.
What’s Included
Build and Modify the DOM
The DOM layer is built around Document, Element, and Node. Create elements with Document.create_element(), wire them together with append_child(), set markup attributes with Element.set_attribute(), and look nodes up again with Document.get_element_by_id(). Full pages and fragments parse through HTMLDocument.parse() and HTMLDocument.parse_fragment().
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
Apply Stylesheets and Read Computed Styles
CSSStyleSheet.replace_sync() parses stylesheet text, Document.attach_style_sheet() puts it in play, and Element.get_computed_style() resolves the cascade. The result honours selector specificity — here the ID rule beats the class rule:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: blue }")
doc.attach_style_sheet(sheet)
style = el.get_computed_style()
print(style.get_property_value("color"))
Inline Declarations and the Cascade
Each element exposes a live CSSStyleDeclaration through its style property. Inline declarations set with set_property() carry higher specificity than author stylesheet rules, and get_computed_style() reflects that:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
Tokenizer and Tree Builder
When you need more control than a finished tree, the lower layers are public API. Tokenizer.tokenize() and Tokenizer.tokenize_fragment() emit the token stream for a piece of markup, TreeBuilder.run() performs standards-based tree construction, and the parse_html() function goes from markup to tree in one call. These are the same components the high-level parse path uses.
URLs and Character Encodings
URL.parse() and URL.can_parse() handle WHATWG-style URL parsing and validation, with URLSearchParams (append(), get(), has(), delete()) for query strings. For raw bytes, detect_encoding() sniffs the encoding — byte-order marks included — and returns the decoded text alongside a confidence level, while get_canonical_name() normalizes encoding labels:
from aspose_html.encoding.detection import detect_encoding
result = detect_encoding(b"\xef\xbb\xbf<p>x</p>")
print(result.encoding)
print(result.confidence)
print(result.text)
Quick Start
Install from PyPI:
pip install aspose-html-foss
Then build a document, style it, and read the computed result:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("#bar { color: blue }")
doc.attach_style_sheet(sheet)
style = el.get_computed_style()
print(style.get_property_value("color"))
Open Source & Licensing
Aspose.HTML FOSS for Python is released under the MIT license, so commercial use, modification, and redistribution are all permitted. The source code is available on GitHub and the package is published on PyPI as aspose-html-foss.