Skip to content

BUILDING URLS - urllib.parse.urljoin()

Python
#!/usr/bin/env python3
"""BUILDING URLS - urllib.parse.urljoin()"""
import urllib.parse
print("Building URLs with urljoin():")
test_cases = [
    ("http://example.com/path/", "page.html"),
    ("http://example.com/path/page.html", "../other.html"),
    ("http://example.com/", "/absolute/path"),
    ("http://example.com/path/", "http://other.com/page")
]
for base, relative in test_cases:
    result = urllib.parse.urljoin(base, relative)
    print(f"\n  Base: {base}")
    print(f"  Relative: {relative}")
    print(f"  Result: {result}")