Skip to content
Advertisement

URL query parameters to dict python

Is there a way to parse a URL (with some python library) and return a python dictionary with the keys and values of a query parameters part of the URL?

For example:

JavaScript

expected return:

JavaScript

Advertisement

Answer

Use the urllib.parse library:

JavaScript

The urllib.parse.parse_qs() and urllib.parse.parse_qsl() methods parse out query strings, taking into account that keys can occur more than once and that order may matter.

If you are still on Python 2, urllib.parse was called urlparse.

Advertisement