Percent URL encoding using python

When I enter the URL on maps.google.com, for example https://dl.dropbox.com/u/94943007/file.kml , it will encode this URL into:

https:%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml 

I am wondering what is called this encoding, and is there any way to encode such a url using python?

I tried this:

The process is called URL encoding :

 >>> urllib.quote('https://dl.dropbox.com/u/94943007/file.kml', '') 'https%3A%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml' 

but did not get the expected results:

 'https%3A//dl.dropbox.com/u/94943007/file.kml' 

what I need:

 https:%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml 

How to encode this url correctly?

documentation here:

https://developers.google.com/maps/documentation/webservices/

it says:

All URL encoded characters are encoded using the '%' character and a two-character hexadecimal value corresponding to their UTF-8 character. For example, 上桷 + δΈ­εœ‹ in UTF-8 will be encoded URL as% E4% B8% 8A% E6% B5% B7% 2B% E4% B8% AD% E5% 9C% 8B. String? and the Mysterians will be URL encoded as% 3F + and + + Mysterians.

+4
source share
1 answer

Using

 urllib.quote_plus(url, safe=':') 

Since you do not want to encode the colon, you need to indicate that when urllib.quote() called:

 >>> expected = 'https:%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml' >>> url = 'https://dl.dropbox.com/u/94943007/file.kml' >>> urllib.quote(url, safe=':') == expected True 

urllib.quote() accepts the safe keyword argument, which is / by default and indicates which characters are considered safe and therefore need not be encoded. In the first example, you used '' , as a result of which slashes were encoded. The unexpected output that you inserted below where the slashes were not encoded was probably from a previous attempt, when you did not set the safe keyword argument at all.

Overriding the default value '/' and eliminating the colon with ':' is what ultimately gives the desired result.

Change In addition, the API requires spaces to be encoded as plus signs. Therefore, you should use urllib.quote_plus() (whose key argument safe is not equal to '/' by default).

+5
source

All Articles