Python的urllib.quote()和urllib.unquote()的等效Javascript函数

时间:2022-05-20 20:31:09

Are there any equivalent Javascript functions for Python's urllib.quote() and urllib.unquote()?

Python的urllib.quote()和urllib.unquote()是否有任何等效的Javascript函数?

The closest I've come across are escape(), encodeURI(), and encodeURIComponent() (and their corresponding un-encoding functions), but they don't encode/decode the same set of special characters as far as I can tell.

我遇到的最接近的是escape(),encodeURI()和encodeURIComponent()(及其相应的非编码函数),但就我所知,它们不会对同一组特殊字符进行编码/解码。

Thanks,
Cameron

5 个解决方案

#1


59  

For the record:

作为记录:

JavaScript               |  Python
----------------------------------- 
encodeURI(str)           |  urllib.quote(str, safe='~@#$&()*!+=:;,.?/\'');
-----------------------------------
encodeURIComponent(str)  |  urllib.quote(str, safe='~()*!.\'')

#2


6  

OK, I think I'm going to go with a hybrid custom set of functions:

好的,我想我将使用混合自定义函数集:

Encode: Use encodeURIComponent(), then put slashes back in.
Decode: Decode any %hex values found.

编码:使用encodeURIComponent(),然后将斜杠放回。解码:解码找到的任何%十六进制值。

Here's a more complete variant of what I ended up using (it handles Unicode properly, too):

这是我最终使用的更完整的变体(它也正确处理Unicode):

function quoteUrl(url, safe) {
    if (typeof(safe) !== 'string') {
        safe = '/';    // Don't escape slashes by default
    }

    url = encodeURIComponent(url);

    // Unescape characters that were in the safe list
    toUnencode = [  ];
    for (var i = safe.length - 1; i >= 0; --i) {
        var encoded = encodeURIComponent(safe[i]);
        if (encoded !== safe.charAt(i)) {    // Ignore safe char if it wasn't escaped
            toUnencode.push(encoded);
        }
    }

    url = url.replace(new RegExp(toUnencode.join('|'), 'ig'), decodeURIComponent);

    return url;
}


var unquoteUrl = decodeURIComponent;    // Make alias to have symmetric function names

Note that if you don't need "safe" characters when encoding ('/' by default in Python), then you can just use the built-in encodeURIComponent() and decodeURIComponent() functions directly.

请注意,如果在编码时不需要“安全”字符(默认情况下在Python中为'/'),那么您可以直接使用内置的encodeURIComponent()和decodeURIComponent()函数。

Also, if there are Unicode characters (i.e. characters with codepoint >= 128) in the string, then to maintain compatibility with JavaScript's encodeURIComponent(), the Python quote_url() would have to be:

此外,如果字符串中有Unicode字符(即代码点> = 128的字符),那么为了保持与JavaScript的encodeURIComponent()的兼容性,Python quote_url()必须是:

def quote_url(url, safe):
    """URL-encodes a string (either str (i.e. ASCII) or unicode);
    uses de-facto UTF-8 encoding to handle Unicode codepoints in given string.
    """
    return urllib.quote(unicode(url).encode('utf-8'), safe)

And unquote_url() would be:

unquote_url()将是:

def unquote_url(url):
    """Decodes a URL that was encoded using quote_url.
    Returns a unicode instance.
    """
    return urllib.unquote(url).decode('utf-8')

#3


3  

The requests library is a bit more popular if you don't mind the extra dependency

如果你不介意额外的依赖,请求库会更受欢迎

from requests.utils import quote
quote(str)

#4


1  

Try a regex. Something like this:

试试一个正则表达式。像这样的东西:

mystring.replace(/[\xFF-\xFFFF]/g, "%" + "$&".charCodeAt(0));

That will replace any character above ordinal 255 with its corresponding %HEX representation.

这将用相应的%HEX表示替换序号255以上的任何字符。

#5


1  

Python: urllib.quote

Javascript:unescape

I haven't done extensive testing but for my purposes it works most of the time. I guess you have some specific characters that don't work. Maybe if I use some Asian text or something it will break :)

我没有做过大量的测试,但就我的目的而言,它大部分时间都有效。我想你有一些不起作用的特定字符。也许如果我使用一些亚洲文字或其他东西它会打破:)

This came up when I googled so I put this in for all the others, if not specifically for the original question.

当我用谷歌搜索时,这就出现了,所以我把它放在所有其他的,如果不是专门针对原始问题。

#1


59  

For the record:

作为记录:

JavaScript               |  Python
----------------------------------- 
encodeURI(str)           |  urllib.quote(str, safe='~@#$&()*!+=:;,.?/\'');
-----------------------------------
encodeURIComponent(str)  |  urllib.quote(str, safe='~()*!.\'')

#2


6  

OK, I think I'm going to go with a hybrid custom set of functions:

好的,我想我将使用混合自定义函数集:

Encode: Use encodeURIComponent(), then put slashes back in.
Decode: Decode any %hex values found.

编码:使用encodeURIComponent(),然后将斜杠放回。解码:解码找到的任何%十六进制值。

Here's a more complete variant of what I ended up using (it handles Unicode properly, too):

这是我最终使用的更完整的变体(它也正确处理Unicode):

function quoteUrl(url, safe) {
    if (typeof(safe) !== 'string') {
        safe = '/';    // Don't escape slashes by default
    }

    url = encodeURIComponent(url);

    // Unescape characters that were in the safe list
    toUnencode = [  ];
    for (var i = safe.length - 1; i >= 0; --i) {
        var encoded = encodeURIComponent(safe[i]);
        if (encoded !== safe.charAt(i)) {    // Ignore safe char if it wasn't escaped
            toUnencode.push(encoded);
        }
    }

    url = url.replace(new RegExp(toUnencode.join('|'), 'ig'), decodeURIComponent);

    return url;
}


var unquoteUrl = decodeURIComponent;    // Make alias to have symmetric function names

Note that if you don't need "safe" characters when encoding ('/' by default in Python), then you can just use the built-in encodeURIComponent() and decodeURIComponent() functions directly.

请注意,如果在编码时不需要“安全”字符(默认情况下在Python中为'/'),那么您可以直接使用内置的encodeURIComponent()和decodeURIComponent()函数。

Also, if there are Unicode characters (i.e. characters with codepoint >= 128) in the string, then to maintain compatibility with JavaScript's encodeURIComponent(), the Python quote_url() would have to be:

此外,如果字符串中有Unicode字符(即代码点> = 128的字符),那么为了保持与JavaScript的encodeURIComponent()的兼容性,Python quote_url()必须是:

def quote_url(url, safe):
    """URL-encodes a string (either str (i.e. ASCII) or unicode);
    uses de-facto UTF-8 encoding to handle Unicode codepoints in given string.
    """
    return urllib.quote(unicode(url).encode('utf-8'), safe)

And unquote_url() would be:

unquote_url()将是:

def unquote_url(url):
    """Decodes a URL that was encoded using quote_url.
    Returns a unicode instance.
    """
    return urllib.unquote(url).decode('utf-8')

#3


3  

The requests library is a bit more popular if you don't mind the extra dependency

如果你不介意额外的依赖,请求库会更受欢迎

from requests.utils import quote
quote(str)

#4


1  

Try a regex. Something like this:

试试一个正则表达式。像这样的东西:

mystring.replace(/[\xFF-\xFFFF]/g, "%" + "$&".charCodeAt(0));

That will replace any character above ordinal 255 with its corresponding %HEX representation.

这将用相应的%HEX表示替换序号255以上的任何字符。

#5


1  

Python: urllib.quote

Javascript:unescape

I haven't done extensive testing but for my purposes it works most of the time. I guess you have some specific characters that don't work. Maybe if I use some Asian text or something it will break :)

我没有做过大量的测试,但就我的目的而言,它大部分时间都有效。我想你有一些不起作用的特定字符。也许如果我使用一些亚洲文字或其他东西它会打破:)

This came up when I googled so I put this in for all the others, if not specifically for the original question.

当我用谷歌搜索时,这就出现了,所以我把它放在所有其他的,如果不是专门针对原始问题。