views:

523

answers:

2

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!

+10  A: 

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.

Andrew Hare
Wow, perfect. Thanks!
orwellian
A: 

Andrew's answer was really informative and helpful. A less adept way to grab those params would be with a regular expression--something like this:

import re

re_param = re.compile(r'(?P<key>w\+)=(?P<value>w\+)')

url = 'http://somesite.com/?foo=bar&amp;key=val''
params_list = re_param.findall(url)

Also, in your code it looks like you're trying to concatenate a list and tuple--

for param in url[1].split('&'):
    get = get + param.split('=')

You created get as a tuple, but str.split returns a list. Maybe this would fix your code:

for param in url[1].split('&'):
    get = get + tuple(param.split('='))
twneale