如何在python中解码(双重)'url-encoded'字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28431359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:16:47  来源:igfitidea点击:

How to decode a (doubly) 'url-encoded' string in python

pythonurlencodeurldecode

提问by user1986059

Tried decoding a url-encodedstring in the following way

尝试url-encoded以下列方式解码字符串

some_string = 'FireShot3%2B%25282%2529.png'
import urllib
res = urllib.unquote(some_string).decode()
res
u'FireShot3+%282%29.png'

Original string is FireShot3 (2).png. Any help would be appreciated.

原始字符串是FireShot3 (2).png. 任何帮助,将不胜感激。

Answer: urllib.unquote_plus(urllib.unquote_plus(some_string))due to double encoding.

答: urllib.unquote_plus(urllib.unquote_plus(some_string))由于双重编码。

采纳答案by user1986059

Your input is encoded double. Using Python 3:

您的输入被编码为double。使用 Python 3:

urllib.parse.unquote(urllib.parse.unquote(some_string))

Output:

输出:

'FireShot3+(2).png'

now you have the +left.

现在你有+左边了。

Edit:

编辑:

Using Python 2.7 it of course is:

使用 Python 2.7 当然是:

urllib.unquote(urllib.unquote('FireShot3%2B%25282%2529.png'))

回答by user1986059

urllib.unquote_plus(urllib.unquote_plus(some_string)) FireShot3 (2).png

urllib.unquote_plus(urllib.unquote_plus(some_string)) FireShot3 (2).png