[Python] How to Parse URL Query Parameters into a Dictionary

In web application development or scraping, you often need to analyze query parameters at the end of a URL (in the format ?key=value&...) and handle them as a Python dictionary.

You can easily perform this conversion using the parse_qs() function from the standard library urllib.parse module. A key feature to note is that, to handle cases where a single key has multiple values, all values in the dictionary are returned as lists.

目次

Implementation Example: Analyzing a Book Search Query

In this example, we will parse a query string from an online bookstore search URL (containing genre, author, format, etc.) and convert it into a data format that is easy to handle within a program.

Source Code

from urllib import parse

# 1. Query string to analyze
# Usually, this is the part after the '?' in a URL
# Example: genre=mystery&author=Conan+Doyle&format=paperback&format=ebook
# Note: The key 'format' appears twice
query_string = "genre=mystery&author=Conan+Doyle&format=paperback&format=ebook"

# 2. Convert to dictionary using parse_qs()
# URL-encoded parts (like '+' or '%XX') are automatically decoded
query_dict = parse.parse_qs(query_string)

print("--- Parsed Dictionary ---")
print(query_dict)

print("\n--- Accessing Each Value ---")

# NOTE: Values are always stored as "lists"

# Even if there is only one value, it is a list, so retrieve the element with [0]
target_genre = query_dict["genre"][0]
print(f"Genre : {target_genre}")

# Author name (spaces encoded as '+' are restored)
target_author = query_dict["author"][0]
print(f"Author: {target_author}")

# When there are multiple values for the same key (retrieve the whole list)
formats = query_dict["format"]
print(f"Format: {formats}")

Execution Result

--- Parsed Dictionary ---
{'genre': ['mystery'], 'author': ['Conan Doyle'], 'format': ['paperback', 'ebook']}

--- Accessing Each Value ---
Genre : mystery
Author: Conan Doyle
Format: ['paperback', 'ebook']

Explanation

parse_qs(qs, keep_blank_values=False)

This function accepts a Query String and returns a dictionary.

  • Why values become lists: The URL specification allows specifying the same key multiple times, like ?id=1&id=2. To prevent data loss, Python always stores values in a list format ['value'], even if there is only one value.
  • How to retrieve values: To get a standard parameter (where you know there is only one value), you must specify the index, like d['key'][0].
  • keep_blank_values: The default is False, meaning empty parameters (e.g., &flag=&) are ignored. If set to True, they are retrieved as lists containing empty strings.

Supplement: parse_qsl()

If you want to retrieve parameters in order as a list of tuples like [('key', 'val'), ('key', 'val2')] instead of a dictionary, use the parse_qsl() function.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次