1. Products
  2.   Aspose.HTML
  3.   Aspose.HTML FOSS for Python

Aspose.HTML FOSS for Python

Parse HTML into a standards-based DOM, apply CSS, and compute styles from Python — free, open-source, and pure Python.

Open-Source Python Library for HTML Processing

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.

HTML Parsing and DOM Manipulation

  • Parse complete pages with HTMLDocument.parse() or fragments with HTMLDocument.parse_fragment()
  • Build documents programmatically with Document.create_element() and append_child()
  • Look up nodes with Document.get_element_by_id() and read markup attributes via Element.get_attribute()
  • Set attributes with Element.set_attribute() and assign text through Node.text_content
  • Serialize any subtree back to markup with the serialise() function

Who Uses DOM Processing

  • Backend services that clean up or rewrite user-submitted HTML before storage
  • Content pipelines that extract or restructure page fragments at scale
  • Test suites that assert on generated markup structure in CI

CSS Stylesheets and Computed Styles

  • Parse stylesheet text with CSSStyleSheet.replace_sync() and attach it via Document.attach_style_sheet()
  • Set inline declarations through the element style property with CSSStyleDeclaration.set_property()
  • Resolve the cascade — specificity, inline-vs-author rules, and inheritance — with Element.get_computed_style()
  • Read resolved values back with CSSStyleDeclaration.get_property_value()

Who Uses Style Computation

  • Email and newsletter tooling that verifies effective styles before send
  • HTML sanitizers that need to know the style an element actually renders with
  • Developers debugging cascade and specificity behaviour outside a browser

Tokenizer and Tree Construction

  • Drive the HTML tokenizer directly with Tokenizer.tokenize() or Tokenizer.tokenize_fragment()
  • Run standards-based tree construction with TreeBuilder.run()
  • Parse markup straight to a tree with the parse_html() function

Who Uses Low-Level Parsing

  • Linters and analyzers that inspect token streams rather than finished trees
  • Tools that need custom error handling during malformed-markup recovery
  • Educational projects studying how standards-based HTML parsing works

URLs and Character Encodings

  • Parse and validate URLs with URL.parse() and URL.can_parse()
  • Read and edit query strings with URLSearchParamsappend(), get(), has(), delete()
  • Detect byte-stream encodings (BOM and content sniffing) with detect_encoding()
  • Normalize encoding labels to canonical names with get_canonical_name()

Who Uses URL and Encoding Tools

  • Crawlers and fetchers that must resolve and dedupe URLs reliably
  • Ingestion pipelines handling legacy documents with mixed encodings
  • Services that parse or rewrite query parameters server-side

Build a DOM and Resolve the CSS Cascade

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 Styles Take Priority

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

Detect Character Encoding from Bytes

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)

Frequently Asked Questions

What license does Aspose.HTML FOSS for Python use?

The MIT License — commercial use, modification, and redistribution are all permitted; the only requirement is keeping the copyright notice with substantial copies.

Which Python versions are supported, and how do I install?

Python 3.10 or later. Install from PyPI with pip install aspose-html-foss — the implementation is pure Python with no native dependencies.

How do I parse HTML into a document?

Use HTMLDocument.parse() for complete pages or HTMLDocument.parse_fragment() for partial markup; Document is the factory for building trees programmatically.

Does the CSS cascade handle specificity and !important correctly?

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.

Does the library render pages or convert to other formats?

No. The current release focuses on parsing, DOM manipulation, CSS style computation, layout structures, URLs, and encodings — no format conversion or image/PDF rendering.

Is JavaScript executed automatically during parsing?

No. Parsing never runs scripts; evaluation is explicit and opt-in through JSContext with module loading controlled by ModuleRegistry and ModuleLoadPolicy.

  

Support and Learning Resources