[Tutor] urlparse question

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 26 Aug 2002 11:04:24 -0700 (PDT)


On Mon, 26 Aug 2002 Jaco.Smuts@za.didata.com wrote:

> Hello there
>
> I am busy writing a small app to extract keywords (amongst other things)
> from search engine referer urls stored in a mysql database (using apache
> mod_log_sql).
>
> 1 - Please tell me if I'm re-inventing the wheel.

You're reinventing the wheel.  *grin*


> 2 - If not , what I'm struggling with is to get the differnet url
> querystring parameters, what am I missing ?
>
> example:
> the url is
> http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=utf-8&q=south+africa+trav
> el+cape+town

The function cgi.parse_qs() will parse out a query string for you:

###
>>> query_string =
"hl=en&lr=&ie=UTF-8&oe=utf-8&q=south+africa+travel+cape+town">>> import
cgi
>>> dict = cgi.parse_qs(query_string)
>>> dict
{'hl': ['en'], 'ie': ['UTF-8'], 'oe': ['utf-8'],
 'q': ['south africa travel cape town']}
###

See:

    http://www.python.org/doc/lib/node298.html

for more information on cgi.parse_qs().


I am a bit concerned, though, that this function wasn't easy to find; the
url parsing stuff seems distributed among the urlparse and cgi modules ---
I think it would be better if they were consolidated into the urlparse
module!  I'll add a feature request on Sourceforge to see if it's possible
to move that function over to urlparse.


Good luck!