119

I am trying to form URLs from different pieces, and having trouble understanding the behavior of this method. For example:

Python 3.x

from urllib.parse import urljoin

>>> urljoin('some', 'thing')
'thing'
>>> urljoin('http://some', 'thing')
'http://some/thing'
>>> urljoin('http://some/more', 'thing')
'http://some/thing'
>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'
urljoin('http://some/more/', '/thing')
'http://some/thing'

Can you explain the exact behavior of this method?

1
  • 1
    Note to those coming across this question: the above import statement is for Python 3.x. Use "from urlparse import urljoin" for python 2.x.
    – Joe J
    Feb 26, 2014 at 3:20

3 Answers 3

137

The best way (for me) to think of this is the first argument, base is like the page you are on in your browser. The second argument url is the href of an anchor on that page. The result is the final url to which you will be directed should you click.

>>> urljoin('some', 'thing')
'thing'

This one makes sense given my description. Though one would hope base includes a scheme and domain.

>>> urljoin('http://some', 'thing')
'http://some/thing'

If you are on a vhost some, and there is an anchor like <a href='thing'>Foo</a> then the link will take you to http://some/thing

>>> urljoin('http://some/more', 'thing')
'http://some/thing'

We are on some/more here, so a relative link of thing will take us to /some/thing

>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'

Here, we aren't on some/more, we are on some/more/ which is different. Now, our relative link will take us to some/more/thing

>>> urljoin('http://some/more/', '/thing')
'http://some/thing'

And lastly. If on some/more/ and the href is to /thing, you will be linked to some/thing.

5
  • 10
    Thanks for explaining... this kind of behaviour makes look for 'true' urljoin, acting similar to os.path.join
    – Evgeny
    Sep 27, 2017 at 17:58
  • 2
    For those who also just want to add one bit of url onto another, without urljoin's logic, posixpath.join() may work for you.
    – Harabeck
    Dec 8, 2020 at 15:07
  • I like urljoin('http://', 'some/', 'thing') how it ends up with: 'http:///some/' ¯\_(ツ)_/¯
    – seb
    Jun 17, 2021 at 9:33
  • 1
    @seb urljoin is not variadic, the third parameter is a boolean flag Jun 24, 2021 at 15:33
  • Thanks, I see. But still, the "///" is weird.
    – seb
    Jun 25, 2021 at 18:43
15

urllib.parse.urljoin(base, url)

If url is an absolute URL (that is, starting with //, http://, https://, ...), the url’s host name and/or scheme will be present in the result. For example:

>>> urljoin('https://www.google.com', '//www.microsoft.com')
'https://www.microsoft.com'
>>>

otherwise, urllib.parse.urljoin(base, url) will

Construct a full (“absolute”) URL by combining a “base URL” (base) with another URL (url). Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL.

>>> urlparse('http://a/b/c/d/e')
ParseResult(scheme='http', netloc='a', path='/b/c/d/e', params='', query='', fragment='')
>>> urljoin('http://a/b/c/d/e', 'f')
>>>'http://a/b/c/d/f'
>>> urlparse('http://a/b/c/d/e/')
ParseResult(scheme='http', netloc='a', path='/b/c/d/e/', params='', query='', fragment='')
>>> urljoin('http://a/b/c/d/e/', 'f')
'http://a/b/c/d/e/f'
>>>

it grabs the path of the first parameter (base), strips the part after the last / and joins with the second parameter (url).

If url starts with /, it joins the scheme and netloc of base with url

>>>urljoin('http://a/b/c/d/e', '/f')
'http://a/f'
1

A picture is worth a thousand words.

$ python3
Python 3.11.4 (main, Jun 20 2023, 17:23:00) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from urllib.parse import urljoin
>>> urljoin("http://a/b", "c/d")
'http://a/c/d'
>>> urljoin("http://a/b", "/c/d")
'http://a/c/d'
>>> urljoin("http://a/b/", "c/d")
'http://a/b/c/d'
>>> urljoin("http://a/b/", "/c/d")
'http://a/c/d'

The best practice is:

Use the "base" parameter with a trailing slash ("/"), and avoid starting the "url" parameter with a slash ("/").

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.