How to Work with URLs in Python
The URL layer of Aspose.HTML FOSS for Python implements WHATWG-style parsing, so URLs found in documents resolve and normalize the way browsers treat them. This guide covers parsing, validation, query strings, and the failure model, using pip for installation.
Step-by-Step Guide
Step 1: Install the Package
pip install aspose-html-fossStep 2: Import Required Classes
from aspose_html.url import URL, URLSearchParamsStep 3: Parse and Validate
URL.parse() parses absolute or relative URLs (with a base); URL.can_parse() answers validity cheaply without constructing an object; parse failures raise the typed URLParseError.
Step 4: Edit Query Strings
URLSearchParams round-trips query parameters — append(), get(), get_all(), has(), and delete() — preserving multi-value semantics for repeated keys.
Common Issues and Fixes
A relative URL fails to parse.
Relative parsing needs a base URL — supply one to URL.parse().
Only the first value of a repeated key comes back.
get() returns the first match by design; use get_all() for every value.
Components look normalized or percent-encoded unexpectedly. WHATWG normalization at work — the output matches browser behaviour.
Frequently Asked Questions
How do I validate a URL without parsing it?
URL.can_parse() returns a boolean without object construction.
Are query keys case-sensitive?
Yes — keys are matched as-is, consistent with the URL standard.
What error type do parse failures raise?
The typed URLParseError — catch it instead of pre-validating with regular expressions.