URLSearchParams¶
The URLSearchParams class provides utilities for working with URL query strings according to the WHATWG URL Standard.
Overview¶
URLSearchParams implements Python's MutableMapping interface, enabling dictionary-style access alongside WHATWG-compliant methods.
from pywhatwgurl import URLSearchParams
params = URLSearchParams("foo=1&bar=2")
print(params["foo"]) # "1" (Pythonic)
print("bar" in params) # True (Pythonic)
params["baz"] = "3" # Pythonic assignment
del params["bar"] # Pythonic deletion
Pythonic vs WHATWG Methods¶
| Operation | Pythonic (Preferred) | WHATWG Method |
|---|---|---|
| Get value | params["key"] |
params.get("key") |
| Set value | params["key"] = "value" |
params.set("key", "value") |
| Delete | del params["key"] |
params.delete("key") |
| Check exists | "key" in params |
params.has("key") |
| Get length | len(params) |
params.size |
| Iterate | params.items() |
params.entries() |
| To string | str(params) |
params.to_string() |
Constructor¶
Initialize the URLSearchParams object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init
|
Optional[Union[str, Iterable[Sequence[str]], Mapping[str, Union[str, Sequence[str]]], 'URLSearchParams']]
|
Initializing object. Can be a query string, iterable of pairs, mapping (dict), or valid URLSearchParams object. |
None
|
Source code in pywhatwgurl/search_params.py
Reading Parameters¶
get¶
Return the first value associated with the given search parameter.
Note
params[name] or params.get(name) is preferred.
This method is primarily for WHATWG Spec compliance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the parameter to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The first value associated with |
get_all¶
Use this method when a parameter may have multiple values.
Return all values associated with the given search parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the parameter to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, ...]
|
A tuple containing all values associated with |
has¶
Return True if name exists.
If value is provided, return True only if a pair with name and value exists.
Note
name in params is preferred for checking existence of a key.
This method is primarily for WHATWG Spec compliance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the parameter to check. |
required |
value
|
Optional[str]
|
The specific value to check for. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the parameter exists (matching value if provided), False otherwise. |
Modifying Parameters¶
set¶
Set the value associated with a given search parameter to the given value.
If there were several values, delete the others.
Note
params[name] = value is preferred.
This method is primarily for WHATWG Spec compliance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the parameter to set. |
required |
value
|
str
|
The value to set. |
required |
Source code in pywhatwgurl/search_params.py
append¶
Use this method to add multiple values for the same key.
Append a new name-value pair to the query string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the parameter. |
required |
value
|
str
|
The value of the parameter. |
required |
delete¶
Remove all name-value pairs where name is name.
If value is provided, only remove pairs where both name and value match.
Note
For deleting by name, del params[name] is preferred.
This method is primarily for WHATWG Spec compliance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the parameter(s) to remove. |
required |
value
|
Optional[str]
|
The specific value to match. If None, removes all pairs with |
None
|
Source code in pywhatwgurl/search_params.py
sort¶
Sort all name-value pairs by name.
Note
This method is primarily for WHATWG Spec compliance. Valid URLs do not require sorted parameters.
Source code in pywhatwgurl/search_params.py
Iteration¶
entries¶
Return an iterator over all name-value pairs.
Note
params.items() is preferred.
This method is primarily for WHATWG Spec compliance.
Returns:
| Type | Description |
|---|---|
Iterator[Tuple[str, str]]
|
An iterator yielding (name, value) tuples. |
keys¶
values¶
items¶
Pythonic method equivalent to entries().
for_each¶
Iterate through all values.
Note
Pythonic iteration (for k in params) is preferred.
This method is primarily for WHATWG Spec compliance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
_URLSearchParamsForEachCallback
|
Function to call for each element. |
required |
Size and Serialization¶
size¶
Return the total number of name-value pairs.
Note
len(params) is preferred.
This method is primarily for WHATWG Spec compliance.
to_string¶
Return the query string suitable for use in a URL.
Note
str(params) is preferred.
This method is primarily for WHATWG Spec compliance.
Returns:
| Type | Description |
|---|---|
str
|
The serialized query string. |
Special Methods¶
| Method | Usage | Description |
|---|---|---|
__getitem__ |
params["key"] |
Get value (raises KeyError if not found) |
__setitem__ |
params["key"] = "value" |
Set value (replaces existing) |
__delitem__ |
del params["key"] |
Delete key (raises KeyError if not found) |
__contains__ |
"key" in params |
Check if key exists |
__len__ |
len(params) |
Get number of parameters |
__iter__ |
for key in params |
Iterate over keys |
__str__ |
str(params) |
Convert to query string |