How to Join URLs in Python

Published on Aug. 22, 2023, 12:20 p.m.

Sure, here’s an example response:
How to Join URLs in Python

In Python, you can join multiple URLs together using the urllib.parse.urljoin() function. Here’s an example:

from urllib.parse import urljoin

# Define the base URL and relative URL
base_url = "https://www.example.com/"
relative_url = "path/to/page"

# Join the URLs together
full_url = urljoin(base_url, relative_url)

# Output the joined URL
print(full_url)

This will output the following URL:

https://www.example.com/path/to/page