如何在python中将url解码为windows-1251

wd5*_*wd5 2 python

如何在 python 2.7 和 python 3.2 中将 url 解码为 windows-1251?
例子:

a = "??????"
urllib.quote_plus(a)
'%D0%BF%D1%80%D0%B8%D0%BC%D0%B5%D1%80' (unicode)
Run Code Online (Sandbox Code Playgroud)

如何在 windows-1251 (%EF%F0%E8%EC%E5%F0) 中制作它

geo*_*org 5

永远不要使用没有 'u' 前缀的“国际”字符串。没有它,你的“字符串”只是一大块字节,python 不知道如何处理它。有了前缀,一切都很简单:

a = u"??????" 

print urllib.quote_plus(a.encode('utf8'))
## %D0%BF%D1%80%D0%B8%D0%BC%D0%B5%D1%80

print urllib.quote_plus(a.encode('cp1251'))
## %EF%F0%E8%EC%E5%F0
Run Code Online (Sandbox Code Playgroud)