且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Python 中将 URL 字符串转换为普通字符串(%20 为空格等)

更新时间:2023-02-23 10:40:46

对于 python 2:

>>>导入 urllib2>>>打印 urllib2.unquote("%CE%B1%CE%BB%20")αλ

对于python 3:

>>>从 urllib.parse 导入取消引用>>>打印(取消引用(%CE%B1%CE%BB%20"))αλ

这里的代码适用于所有版本:

尝试:从 urllib 导入取消引用除了导入错误:从 urllib.parse 导入取消引用打印(取消引用(%CE%B1%CE%BB%20"))

Is there any way in Python to transform this %CE%B1%CE%BB%20 into this αλ which is its real representation?

For python 2:

>>> import urllib2
>>> print urllib2.unquote("%CE%B1%CE%BB%20")
αλ 

For python 3:

>>> from urllib.parse import unquote
>>> print(unquote("%CE%B1%CE%BB%20"))
αλ

And here's code that works in all versions:

try:
    from urllib import unquote
except ImportError:
    from urllib.parse import unquote

print(unquote("%CE%B1%CE%BB%20"))