Aspose.HTML FOSS for Python is a free, open-source library for working with HTML documents in Python applications. Install it with a single pip install aspose-html-foss command and start parsing markup into a document object model, building and modifying element trees, attaching CSS stylesheets, resolving computed styles, and working with URLs and character encodings — all in pure Python with no browser dependency.
The library exposes a standards-oriented API built around HTMLDocument, Document, Element, and CSSStyleSheet. Parse markup with HTMLDocument.parse(), create and connect nodes with Document.create_element() and append_child(), look elements up with get_element_by_id(), style them through CSSStyleSheet.replace_sync() and the element-level style declaration, and read the result back with Element.get_computed_style(). Lower-level building blocks — the HTML Tokenizer, the TreeBuilder, WHATWG-style URL and URLSearchParams, and the detect_encoding() byte-stream detector — are available when you need direct control.
Aspose.HTML FOSS is MIT licensed and implemented in pure Python, requiring Python 3.10 or later. It runs identically on Windows, Linux, and macOS — including CI runners, Docker containers, and serverless environments. For the enterprise product family, see Aspose.HTML — Enterprise Product Family.
HTMLDocument.parse() or fragments with HTMLDocument.parse_fragment()Document.create_element() and append_child()Document.get_element_by_id() and read markup attributes via Element.get_attribute()Element.set_attribute() and assign text through Node.text_contentserialise() functionCSSStyleSheet.replace_sync() and attach it via Document.attach_style_sheet()style property with CSSStyleDeclaration.set_property()Element.get_computed_style()CSSStyleDeclaration.get_property_value()Tokenizer.tokenize() or Tokenizer.tokenize_fragment()TreeBuilder.run()parse_html() functionURL.parse() and URL.can_parse()URLSearchParams — append(), get(), has(), delete()detect_encoding()get_canonical_name()Create a document, attach a stylesheet, and read the computed style — the ID selector wins over 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 carry higher specificity than author stylesheet rules:
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"))
Sniff the encoding of a raw byte stream, BOM included, and get decoded text back:
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)
The MIT License — commercial use, modification, and redistribution are all permitted; the only requirement is keeping the copyright notice with substantial copies.
Python 3.10 or later. Install from PyPI with pip install aspose-html-foss — the implementation is pure Python with no native dependencies.
Use HTMLDocument.parse() for complete pages or HTMLDocument.parse_fragment() for partial markup; Document is the factory for building trees programmatically.
Yes — computed values follow selector specificity, inline declarations outrank author rules, !important author rules outrank non-important inline declarations, and inheritance flows parent to child.
No. The current release focuses on parsing, DOM manipulation, CSS style computation, layout structures, URLs, and encodings — no format conversion or image/PDF rendering.
No. Parsing never runs scripts; evaluation is explicit and opt-in through JSContext with module loading controlled by ModuleRegistry and ModuleLoadPolicy.