跳到内容
🛠️ToolsShed

URL差异比较器

比较两个URL并突出显示各组件的差异。

关于此工具

URL差异检查工具是一个实用的工具,可以并排比较两个URL,并立即发现其变化之处。无论是调试重定向链、审查URL迁移,还是验证跟踪参数是否正确应用,此工具都会将每个URL分解为其核心组件——协议、主机、端口、路径、查询字符串和片段——并精确突出显示它们的差异。当您需要理解URL在系统的不同阶段或开发与生产环境间如何演变时,此工具特别有用。

只需将两个URL粘贴到输入字段中,然后单击比较按钮。该工具显示一个详细的表格,其中显示两个URL的每个组件以及状态指示器:仅在第一个URL中存在的组件用绿色突出显示,仅在第二个中的用红色突出显示,差异用黄色显示,相同部分用灰色表示。在主要组件下方,有一个单独的部分列出任一URL中找到的所有查询参数,使您可以一目了然地识别添加、删除或更改的参数。

常见问题

代码实现

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.