Join a base URL with another URL in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# Join a base URL with another URL in Python

Use the urljoin method from the urllib.parse module to join a base URL with another URL.

The urljoin method constructs a full (absolute) URL by combining a base URL with another URL.

main.py
from urllib.parse import urljoin base_url = 'https://bobbyhadz.com' path = 'images/static/cat.jpg' # โœ… Join a base URL with another URL result = urljoin(base_url, path) # ๐Ÿ‘‡๏ธ https://bobbyhadz.com/images/static/cat.jpg print(result) # --------------------------------------- # โœ… Join URL path components when constructing a URL # ๐Ÿ‘‡๏ธ /global/images/static/dog.png print(urljoin('/global/images/', 'static/dog.png'))

join base url with another url

The code for this article is available on GitHub

If you have multiple URL components, use the posixpath module to join them before passing them to the urljoin() method.

main.py
import posixpath from urllib.parse import urljoin base_url = 'https://bobbyhadz.com' path_1 = 'images' path_2 = 'static' path_3 = 'cat.jpg' path = posixpath.join(path_1, path_2, path_3) print(path) # ๐Ÿ‘‰๏ธ 'images/static/cat.jpg' result = urljoin(base_url, path) # ๐Ÿ‘‡๏ธ https://bobbyhadz.com/images/static/cat.jpg print(result)

using posixpath to join path components

The code for this article is available on GitHub

The urllib.parse.urljoin() method takes a base URL and another URL as parameters and constructs a full (absolute) URL by combining them.

# Joining URL path components when constructing a URL

You can also use the urljoin method to join URL path components when constructing a URL.

main.py
from urllib.parse import urljoin # โœ… Join URL path components # ๐Ÿ‘‡๏ธ /global/images/static/dog.png print(urljoin('/global/images/', 'static/dog.png'))

joining url path components when constructing url

Make sure the output you get is what you expect because the urljoin() method can be a bit confusing when working with URL components that don't end in a forward slash /.

Here is an example.

main.py
from urllib.parse import urljoin # ๐Ÿ‘‡๏ธ /global/static/dog.png print(urljoin('/global/images', 'static/dog.png'))

Notice that the method stripped images from the first component before joining the second component.

The method behaves as expected when the first component ends with a forward slash.

main.py
from urllib.parse import urljoin # ๐Ÿ‘‡๏ธ /global/images/static/dog.png print(urljoin('/global/images/', 'static/dog.png'))
The code for this article is available on GitHub

You might also notice confusing behavior if the second component starts with a forward slash.

main.py
from urllib.parse import urljoin # ๐Ÿ‘‡๏ธ /static/dog.png print(urljoin('/global/images', '/static/dog.png'))

When the second component starts with a forward slash, it is assumed to start at the root.

# Joining URL path components with posixpath.join()

The posixpath.join() method is a bit more predictable and could also be used to join URL path components.

main.py
import posixpath # ๐Ÿ‘‡๏ธ /global/images/static/dog.png print(posixpath.join('/global/images', 'static/dog.png')) # ๐Ÿ‘‡๏ธ /global/images/static/dog.png print(posixpath.join('/global/images/', 'static/dog.png')) # ๐Ÿ‘‡๏ธ /static/dog.png print(posixpath.join('/global/images', '/static/dog.png'))

joining url path components with posixpath join

The code for this article is available on GitHub

The posixpath.join() method can also be passed more than 2 paths.

main.py
import posixpath # ๐Ÿ‘‡๏ธ /global/images/static/dog.png print(posixpath.join('/global', 'images', 'static', 'dog.png'))

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev