如何比较 URI 编码的字符串?

How to compare URI encoded strings?

我接受了一家成长型初创公司的面试。其中一个问题是关于 URI 编码。

W3.org 表示这些URI是相同的。 我用b93改变了abc。我明白了

>>> url4 = "http://b93.com:80/~smith/home.html"
>>> url5 = "http://b93.com/%7Esmith/home.html"
>>> urllib.parse.quote(url4)
'http%3A//b93.com%3A80/~smith/home.html'
>>> urllib.parse.quote(url5)
'http%3A//b93.com/%257Esmith/home.html'

如何比较编码字符串以获得正确的信息? 如何进行进一步测试?

我也用 encodeURIComponenet() 尝试过 JS

var p1 = encodeURIComponent("http://b93.com:80/~smith/home.html");
var p2 = encodeURIComponent("http://b93.com/%7Esmith/home.html");

console.log(p1);
console.log(p2);

输出

http%3A%2F%2Fb93.com%3A80%2F~smith%2Fhome.html
http%3A%2F%2Fb93.com%2F%257Esmith%2Fhome.html

编辑已解决

deceze 建议我规范化 URL Node.Js代码

var normalizeUrl = require('normalize-url');

var n1 = normalizeUrl("http://b93.com:80/~smith/home.html");
var n2 = normalizeUrl("http://b93.com/%7Esmith/home.html");

console.log(n1);
console.log(n2);

var p1 = encodeURIComponent(n1);
var p2 = encodeURIComponent(n2);

console.log(p1);
console.log(p2);

工作正常

http://b93.com/~smith/home.html
http://b93.com/~smith/home.html
http%3A%2F%2Fb93.com%2F~smith%2Fhome.html
http%3A%2F%2Fb93.com%2F~smith%2Fhome.html

一种方法是首先确保比较未加引号的 URL(通过使用 urllib.parse.unquote 而不是 urllib.parse.quote)。然后,您可以使用urllib.parse.urlparse提取URL的主要部分并进行比较。

from urllib.parse import unquote, urlparse
url4 = "http://b93.com:80/~smith/home.html"
url4 = unquote(url4)
url5 = "http://b93.com/%7Esmith/home.html"
url5 = unquote(url5)
u4 = urlparse(url4)
u5 = urlparse(url5)
if u4.scheme == u5.scheme and u4.hostname == u5.hostname and u4.path == u5.path:
    print('equal')
else:
    print('different')

确实,您可能还想通过使用 scheme 定义端口来比较端口 port is None