Skip to content

URL PARSING - urllib.parse.urlparse()

Python
#!/usr/bin/env python3
"""URL PARSING - urllib.parse.urlparse()"""
import urllib.parse
print("URL Parsing:")
urls = [
    "https://www.example.com:8080/path/to/page?key=value#section",
    "http://user:pass@host.com/path",
    "ftp://ftp.example.com/file.txt"
]
for url in urls:
    print(f"\n  URL: {url}")
    parsed = urllib.parse.urlparse(url)
    print(f"    scheme: {parsed.scheme}")
    print(f"    netloc: {parsed.netloc}")
    print(f"    path: {parsed.path}")
    print(f"    params: {parsed.params}")
    print(f"    query: {parsed.query}")
    print(f"    fragment: {parsed.fragment}")