URL¶
The URL class represents a parsed URL and provides methods to inspect and manipulate its components according to the WHATWG URL Standard.
Overview¶
from pywhatwgurl import URL
url = URL("https://example.com:8080/path?q=python#section")
print(url.hostname) # "example.com"
print(url.search_params["q"]) # "python"
Constructor¶
Parse url relative to base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL string to parse. |
required |
base
|
Optional[str]
|
An optional base URL string. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parsing fails. |
Source code in pywhatwgurl/url.py
Static Methods¶
These methods provide alternative ways to parse URLs without raising exceptions.
parse¶
Parse url relative to base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL string to parse. |
required |
base
|
Optional[str]
|
An optional base URL string. |
None
|
Returns:
| Type | Description |
|---|---|
Optional['URL']
|
A new URL object, or None if parsing fails. |
can_parse¶
Return True if url (relative to base) can be parsed, False otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL string to check. |
required |
base
|
Optional[str]
|
An optional base URL string. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the URL is valid, False otherwise. |
URL Components¶
All component properties are readable and writable (except origin and search_params which are read-only).
WHATWG Spec Compliance
These properties correspond directly to the URL class interface in the WHATWG URL Standard.
href¶
origin¶
protocol¶
username¶
password¶
host¶
hostname¶
port¶
pathname¶
search¶
search_params¶
hash¶
Serialization Methods¶
Pythonic Usage
Prefer str(url) over url.to_string(). These methods exist for WHATWG spec compliance.
to_string¶
to_json¶
Return the href, for JSON serialization.
Note
This method is primarily for WHATWG Spec compliance.
Returns:
| Type | Description |
|---|---|
str
|
The full URL string. |
Special Methods¶
| Method | Usage | Description |
|---|---|---|
__str__ |
str(url) |
Returns the full URL string |
__repr__ |
repr(url) |
Returns URL('...') representation |
__eq__ |
url1 == url2 |
Compares URLs by href |
__hash__ |
— | None (URLs are mutable, not hashable) |