%uxxxx
是(Py 3)/ (Py2)不支持的非标准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>'
但您应尽可能避免完全使用编码。