How can I convert a URL query string into a list of tuples using Python?

parsing, python, url

Solution

I believe you are looking for the `urlparse` module.

This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”

Here is an example:

from urlparse import urlparse, parse_qsl

url = 'http://somesite.com/?foo=bar&key=val'
print parse_qsl(urlparse(url)[4])

Output:

[('foo', 'bar'), ('key', 'val')]

In this example I first use the `urlparse` function to parse the entire URL then I use the `parse_qsl` function to break the querystring (the fifth element returned from `urlparse`) into a list of tuples.

Problem

I am struggling to convert a url to a nested tuple. ``` # Convert this string str = 'http://somesite.com/?foo=bar&key=val' # to a tuple like this: [(u'foo', u'bar'), (u'key', u'val')] ``` I assume I need to be doing something like: ``` url = 'http://somesite.com/?foo=bar&key=val' url = url.split('?') get = () for param in url[1].split('&'): get = get + param.split('=') ``` What am I doing wrong? Thanks!

Original source