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.

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

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

Copy>>> 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

Copy>>> 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

Copy>>> 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

Copy>>> 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.

Answer from sberry on Stack Overflow
🌐
Python
docs.python.org › 3 › library › urllib.parse.html
urllib.parse — Parse URLs into components
>>> from urllib.parse import urljoin >>> urljoin('http://www.cwi.nl/~guido/Python.html', 'FAQ.html') 'http://www.cwi.nl/~guido/FAQ.html'
Top answer
1 of 3
157

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.

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

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

Copy>>> 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

Copy>>> 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

Copy>>> 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

Copy>>> 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.

2 of 3
17

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:

Copy>>> 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.

Copy>>> 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

Copy>>>urljoin('http://a/b/c/d/e', '/f')
'http://a/f'
🌐
GitHub
github.com › python › cpython › issues › 96015
urljoin works incorrectly for two path-relative URLs involving . and .. · Issue #96015 · python/cpython
August 16, 2022 - stdlibStandard Library Python modules ... feature request or enhancement ... urllib.parse.urljoin is usually used to join a normalized absolute URL with a relative URL, and it generally works for that purpose....
Author   python
🌐
O'Reilly
oreilly.com › library › view › python-in-a › 0596001886 › re767.html
urljoin - Python in a Nutshell [Book]
March 3, 2003 - NameurljoinSynopsisurljoin(base_url_string,relative_url_string)Returns a URL string u, obtained by joining relative_url_string, which may be relative, with base_url_string. The... - Selection from Python in a Nutshell [Book]
Author   Alex Martelli
Published   2003
Pages   656
🌐
DEV Community
dev.to › sethmlarson › why-urls-are-hard-path-parameters-and-urlparse-c2n
Why URLs are Hard: Path Parameters and urlparse - DEV Community
April 11, 2020 - How I discovered an almost entirely unused URL feature from a mysterious API in Pythons urlparse function. Tagged with python, todayilearned, url.
🌐
Python.org
discuss.python.org › ideas
URLLib Join Behavior - Ideas - Discussions on Python.org
March 29, 2025 - This proposes that urllib.parse.urljoin('https://example.com/thing', 'v1') should resolve to https://example.com/thing/v1 rather than the current behaviour which resolves to https://example.com/v1 without warning.
Find elsewhere
🌐
Read the Docs
stackless.readthedocs.io › en › 2.7-slp › library › urlparse.html
20.16. urlparse — Parse URLs into components — Stackless-Python 2.7.15 documentation
>>> from urlparse import urljoin >>> urljoin('http://www.cwi.nl/~guido/Python.html', 'FAQ.html') 'http://www.cwi.nl/~guido/FAQ.html' The allow_fragments argument has the same meaning and default as for urlparse(). Note · If url is an absolute URL (that is, starting with // or scheme://), the url’s host name and/or scheme will be present in the result.
🌐
Dojo-yeswehack
dojo-yeswehack.com › learn › python-pitfalls › urljoin
Python Pitfalls - urljoin
Python's function urllib.parse.urljoin is used to construct an absolute URL by combining a base URL. It uses components from the base URL, such as the path, to provide missing components in the relative URL.
🌐
Medium
medium.com › @glasshost › join-a-base-url-with-another-url-in-python-549b6506e414
Join a base URL with another URL in Python | by Glasshost | Medium
May 4, 2023 - Joining a base URL with another URL is a common task in web development. Python provides a convenient way to achieve this using the `urljoin()` method from the `urllib.parse` module.
🌐
Saeed Esmaili
saeedesmaili.com › posts › til: simplifying url parsing with python's urlparse library
TIL: Simplifying URL Parsing with Python's urlparse Library | Saeed Esmaili
June 25, 2023 - While looking into this issue, I needed to compare the URLs of the saved items across both platforms. My initial plan was to use regex patterns to split each URL to different parts. However, I stumbled upon Python’s urlparse library .
🌐
Python
bugs.python.org › issue9721
Issue 9721: urlparse.urljoin() cuts off last base character with semicolon at url start - Python tracker
August 31, 2010 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/53930
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › urlparse.html
urlparse — Python Standard Library
Parse (absolute and relative) URLs · urlparse module is based upon the following RFC specifications
🌐
Python
bugs.python.org › issue18828
Issue 18828: urljoin behaves differently with custom and standard schemas - Python tracker
August 25, 2013 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/63028
🌐
sebhastian
sebhastian.com › python-join-urls
Learn how to join URLs in Python | sebhastian
April 25, 2023 - When you need to join multiple URL segments into a single full URL, you can use the urljoin() function from the urllib.parse module.
🌐
Tutorialspoint
tutorialspoint.com › python › python_url_processing.htm
Python - URL Processing
This tutorial introduces urllib basics to help you start using it. Improve your skills in web scraping, fetching data, and managing URLs with Python using urllib.
🌐
ChristopherGS
christophergs.com › python › 2016 › 12 › 03 › python-urllib-parse
Using urllib.parse in Python
December 3, 2016 - base_url = 'https://docs.python.org' addition = '3/library/urllib.parse.html#module-urllib.parse' url = urljoin(base_url, addition) url # 'https://docs.python.org/3/library/urllib.parse.html#module-urllib.parse' If the URL has a fragment, this function splits the fragment and the rest of the URL, returning a tuple of the two values ·
🌐
Python
bugs.python.org › issue22118
Issue 22118: urljoin fails with messy relative URLs - Python tracker
August 1, 2014 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/66316