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?

1
  • Why don't you just concatenate them? Commented Mar 8, 2011 at 12:59

4 Answers 4

5

You could use urlparse.urlunparse :

import urlparse
parsed = list(urlparse.urlparse('http://example.com/mypage'))
parsed[4] = 'name=joe'
urlparse.urlunparse(parsed)
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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.

Comments

0

Are you sure? On Python 2.7:

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

2 Comments

Damn, it seems to be a Python 2.5 bug! It's fixed in Python 2.6. What can I do? I can't upgrade to 2.6, this is on GAE.
I can confirm that it works as expected on Python 2.3 but not 2.4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.