query-string

raw JSON →
2020.12.3 verified Mon Apr 27 auth: no python deprecated

A lightweight Python library for parsing and manipulating URL query strings. The current version is 2020.12.3, released in December 2020. Development appears inactive; no new releases since then.

pip install query-string==2020.12.3
error AttributeError: module 'query_string' has no attribute 'parse'
cause Importing the module itself instead of the function.
fix
Use 'from query_string import parse'.
error ValueError: Invalid URL
cause Passing only a query string (e.g., 'foo=1') without a full URL.
fix
Ensure you pass a full URL like 'http://example.com?foo=1'.
gotcha The parse() function expects a full URL string, not just the query part. If you pass only 'key=value', it may fail or return unexpected results.
fix Always pass a full URL including scheme and host, or prefix with 'http://example.com/?'.
deprecated This library is effectively deprecated. No releases since 2020. The author recommends using urllib.parse (stdlib) or the requests library instead.
fix Use 'from urllib.parse import urlparse, parse_qs' or 'import requests'.
gotcha The library returns values as strings. It does not perform type conversion. Query parameters like 'num=42' will be string '42', not integer 42.
fix Convert values manually after parsing.
breaking In version 2020.12.3, the parse function behavior changed regarding duplicate keys. Previously, it returned the last value; now it returns a list of values for duplicate keys.
fix If you expect a single value, use dict comprehension to take the first or last element from the list.

Parse a URL query string into a dictionary.

from query_string import parse

url = "http://example.com?foo=1&bar=2"
params = parse(url)
print(params)  # {'foo': '1', 'bar': '2'}