The urllib.parse.quote function (urllib.quote function in Python2) used for URL encoding does not encode the path delimiter (/) by default.
>>> help(urllib.parse.quote)
#The character string specified by the argument safe is excluded from encoding.
quote(string, safe='/', encoding=None, errors=None)
quote('abc def') -> 'abc%20def'
(…)
By default, the quote function is intended for quoting the path
section of a URL. Thus, it will not encode '/'. This character
is reserved, but in typical usage the quote function is being
called on a path where the existing slash characters are used as
reserved characters.
Therefore, when encoding a character string including a URL with Twitter API etc., it is necessary to set the second argument (or named argument safe) to '' (empty string)
.
quote_safe
#Path delimiter(/)Is not encoded
>>> urllib.parse.quote('http://hoge.com/api')
'http%3A//hoge.com/api'
#Path delimiter(/)Is encoded
>>> urllib.parse.quote('http://hoge.com/api', '')
'http%3A%2F%2Fhoge.com%2Fapi'
>>> urllib.parse.quote('http://hoge.com/api', safe='')
'http%3A%2F%2Fhoge.com%2Fapi'
Recommended Posts