parametrize_url() adding parameters to URLs

14 January 2005   4 comments   Python

Powered by Fusion×

I needed to write this little function because I need to add some parameters to a URL that I was going to open with urllib2. The benefit with this script is that it can combine a any URL with some structured parameters. The URL could potentially already contain a query string (aka CGI parameters). Here's how to use it if it was placed in a file called '':

>>> from urlfixer import parametrize_url
>>> parametrize_url('',
                    any='one', tv="b b c")

The function needed some extra attention (read hack) if the starting url was of the form which is non-standard. The standard way would be You can download or read it here:

from urlparse import urlparse, urlunparse
from urllib import urlencode

def parametrize_url(url, **params):
   """ don't just add the **params because the url
   itself might contain CGI variables embedded inside
   the string. """
   url_parsed = list(urlparse(url))

   encoded = urlencode(params)
   qs = url_parsed[4]
   if encoded:
       if qs:
           qs += '&'+encoded
           qs = encoded
   netloc = url_parsed[1]
   if netloc.find('?')>-1:
       url_parsed[1] = url_parsed[1][:netloc.find('?')]
       if qs:
           qs = netloc[netloc.find('?')+1:]+'&'+qs
           qs = netloc[netloc.find('?')+1:]

   url_parsed[4] = qs

   url = urlunparse(url_parsed)
   return url


look i have a problem i need to create a unique parameter like a id for a url and that parameter get to a form man i dont know how to do it so i need your help
Hello, nice site look this:
As '?' cannot be in url_parsed.netloc, 'netloc.find('?') > -1' is always false, so that block is useless.

Using '.find()' is discouraged, the Pythonic idiom is 'if "?" in netloc'.
I guess the hack was necessary exactly becuse the non-standard '' form. As of Python 2.5 this is parsed correctly:

>>> u = urlparse('')
>>> u.netloc
>>> u.query

Your email will never ever be published

Related posts

Gmail spam filter 13 January 2005
niceboolean() - converts what you say to what you mean 21 January 2005
Related by keywords:
Add links to a text with URLs 03 November 2004
Private functions in Javascript? 29 April 2006
Smurl from Python 22 September 2005
Quick URLs for some pages 18 January 2004