Equivalent urllib.parse.unquote() in python 2.7

Viewed 5787

I import urlparse instead of urllib.parse in python 2.7 but getting AttributeError: 'function' object has no attribute 'unquote'

File "./URLDefenseDecode2.py", line 40, in decodev2
    htmlencodedurl = urlparse.unquote(urlencodedurl)

What is the equivalent urllib.parse.unquote() in python 2.7 ?

2 Answers

The semantics of Python 3 urllib.parse.unquote are not the same as Python 2 urllib.unqote, especially when dealing with non-ascii strings.

The following code should allow you to always use the newer semantics of Python 3 and eventually you can just remove it, when you no longer need to support Python 2.

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

    # polyfill. This behaves the same as urllib.parse.unquote on Python 3
    def unquote(string, encoding='utf-8', errors='replace'):
        if isinstance(string, bytes):
            raise TypeError("a bytes-like object is required, not '{}'".format(type(string)))

        return stdlib_unquote(string.encode(encoding)).decode(encoding, errors=errors)
Related