Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I percent-encode URL parameters in Python?

If I do

url = "http://example.com?p=" + urllib.quote(query) 
  1. It doesn't encode / to %2F (breaks OAuth normalization)
  2. It doesn't handle Unicode (it throws an exception)

Is there a better library?

like image 864
Paul Tarjan Avatar asked Nov 08 '09 02:11

Paul Tarjan


People also ask

How do I encode a percentage in python?

The quote() function encodes space characters to %20 . If you want to encode space characters to plus sign ( + ), then you can use another function named quote_plus provided by urllib.

How do I encode a URL percentage?

URL Encoding (Percent Encoding)URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

Does Python request encode URL?

In Python, we can URL encode a query string using the urlib. parse module, which further contains a function urlencode() for encoding the query string in URL. The query string is simply a string of key-value pairs.


1 Answers

Python 2

From the documentation:

urllib.quote(string[, safe]) 

Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of the URL.The optional safe parameter specifies additional characters that should not be quoted — its default value is '/'

That means passing '' for safe will solve your first issue:

>>> urllib.quote('/test') '/test' >>> urllib.quote('/test', safe='') '%2Ftest' 

About the second issue, there is a bug report about it. Apparently it was fixed in Python 3. You can workaround it by encoding as UTF-8 like this:

>>> query = urllib.quote(u"Müller".encode('utf8')) >>> print urllib.unquote(query).decode('utf8') Müller 

By the way, have a look at urlencode.

Python 3

In Python 3, the function quote has been moved to urllib.parse:

>>> import urllib.parse >>> print(urllib.parse.quote("Müller".encode('utf8'))) M%C3%BCller >>> print(urllib.parse.unquote("M%C3%BCller")) Müller 
like image 157
Nadia Alramli Avatar answered Oct 19 '22 22:10

Nadia Alramli