🛠️ToolsShed

URL Diff Checker

Compare two URLs and highlight differences in each component.

Frequently Asked Questions

Code Implementation

from urllib.parse import urlparse, parse_qs

def parse_url_components(url: str) -> dict:
    p = urlparse(url)
    return {
        "scheme":   p.scheme,
        "host":     p.netloc,
        "path":     p.path,
        "query":    parse_qs(p.query),
        "fragment": p.fragment,
    }

def url_diff(url1: str, url2: str) -> dict:
    a = parse_url_components(url1)
    b = parse_url_components(url2)
    diff = {}
    for key in a:
        if a[key] != b[key]:
            diff[key] = {"from": a[key], "to": b[key]}
    return diff

url_a = "https://example.com/search?q=hello&page=1#results"
url_b = "https://example.com/search?q=world&page=2#top"

differences = url_diff(url_a, url_b)
if differences:
    print("Differences found:")
    for component, change in differences.items():
        print(f"  {component}:")
        print(f"    from: {change['from']}")
        print(f"    to:   {change['to']}")
else:
    print("URLs are identical")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.