Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's `urlparse`: Adding GET keywords to a URL

I'm doing this:

urlparse.urljoin('http://example.com/mypage', '?name=joe')

And I get this:

'http://example.com/?name=joe'

While I want to get this:

'http://example.com/mypage?name=joe'

What am I doing wrong?

like image 823
Ram Rachum Avatar asked Mar 08 '11 12:03

Ram Rachum


2 Answers

You could use urlparse.urlunparse :

import urlparse
parsed = list(urlparse.urlparse('http://example.com/mypage'))
parsed[4] = 'name=joe'
urlparse.urlunparse(parsed)
like image 153
jd. Avatar answered Sep 28 '22 22:09

jd.


You're experiencing a known bug which affects Python 2.4-2.6.

If you can't change or patch your version of Python, @jd's solution will work around the issue.

However, if you need a more generic solution that works as a standard urljoin would, you can use a wrapper method which implements the workaround for that specific use case, and default to the standard urljoin() otherwise.

For example:

import urlparse

def myurljoin(base, url, allow_fragments=True):
    if url[0] != "?": 
        return urlparse.urljoin(base, url, allow_fragments)
    if not allow_fragments: 
        url = url.split("#", 1)[0]
    parsed = list(urlparse.urlparse(base))
    parsed[4] = url[1:] # assign params field
    return urlparse.urlunparse(parsed)
like image 21
Shawn Chin Avatar answered Sep 28 '22 23:09

Shawn Chin