urllib.unquote无法正确解码u

2024-04-26 08:37:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以在python shell中执行以下操作:

>>> import urllib
>>> s='https://www.microsoft.com/de-at/store/movies/american-pie-pr%C3%A4sentiert-nackte-tatsachen/8d6kgwzl63ql'
>>> print urllib.unquote(s)
https://www.microsoft.com/de-at/store/movies/american-pie-präsentiert-nackte-tatsachen/8d6kgwzl63ql

但是,如果我在python程序中执行此操作,它会错误地解码url:

^{pr2}$

为什么在程序中没有正确地解码,但是在pythonshell中呢?在


Tags: storehttps程序comwwwdeurllibmovies
1条回答
网友
1楼 · 发布于 2024-04-26 08:37:30

以下是解决该问题的方法:

url = urllib.unquote(str(res.url)).decode('utf-8', 'ignore')

res.url是一个unicode字符串,但似乎不能很好地与urllib.unquote配合使用。所以解决方案是首先将它转换成字符串(就像它在python解释器中的样子),然后decode将其转换成Unicode。在

相关问题 更多 >