Javascript unescape()与Python urllib.unquote()
问题内容:
通过阅读各种文章,似乎JavaScript的unescape()
功能等同于Python
urllib.unquote()
,但是当我同时测试这两者时,我得到了不同的结果:
在浏览器控制台中:
unescape('%u003c%u0062%u0072%u003e');
输出: <br>
在Python解释器中:
import urllib
urllib.unquote('%u003c%u0062%u0072%u003e')
输出: %u003c%u0062%u0072%u003e
我希望Python也能返回<br>
。关于我在这里缺少什么的任何想法?
谢谢!
问题答案:
%uxxxx
是(Py 3)/ (Py 2)不支持的非标准URL编码方案。urllib.parse.unquote()``urllib.unquote()
它只是ECMAScript ECMA-262第三版的一部分。该格式被W3C拒绝,并且从不作为RFC的一部分。
您可以使用正则表达式转换此类代码点:
try:
unichr # only in Python 2
except NameError:
unichr = chr # Python 3
re.sub(r'%u([a-fA-F0-9]{4}|[a-fA-F0-9]{2})', lambda m: unichr(int(m.group(1), 16)), quoted)
这将同时解码%uxxxx
和%uxx
ECMAScript 3rd ed可以解码的形式。
演示:
>>> import re
>>> quoted = '%u003c%u0062%u0072%u003e'
>>> re.sub(r'%u([a-fA-F0-9]{4}|[a-fA-F0-9]{2})', lambda m: chr(int(m.group(1), 16)), quoted)
'<br>'
>>> altquoted = '%u3c%u0062%u0072%u3e'
>>> re.sub(r'%u([a-fA-F0-9]{4}|[a-fA-F0-9]{2})', lambda m: chr(int(m.group(1), 16)), altquoted)
'<br>'
但您应尽可能避免完全使用编码。