5

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?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

4 Answers4

5

You could use urlparse.urlunparse :

import urlparse
parsed = list(urlparse.urlparse('http://example.com/mypage'))
parsed[4] = 'name=joe'
urlparse.urlunparse(parsed)
jd.
  • 10,678
  • 3
  • 46
  • 55
1

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)
Community
  • 1
  • 1
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
1

I solved it by bundling Python 2.6's urlparse module with my project. I also had to bundle namedtuple which was defined in collections, since urlparse uses it.

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
0

Are you sure? On Python 2.7:

>>> import urlparse
>>> urlparse.urljoin('http://example.com/mypage', '?name=joe')
'http://example.com/mypage?name=joe'
payne
  • 13,833
  • 5
  • 42
  • 49