您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Javascript unescape()与Python urllib.unquote()

Javascript unescape()与Python urllib.unquote()

%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%uxxECMAScript 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>'

但您应尽可能避免完全使用编码。

python 2022/1/1 18:32:30 有193人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶