Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a url string to safe characters with python?

Tags:

Suppose I have the following string:

"http://earth.google.com/gallery/kmz/women_for_women.kmz?a=large" 

Is there some function or module to be able to convert a string like the above to a string below where all the characters are changed to be compliant with a url:

"http%3A%2F%2Fearth.google.com%2Fgallery%2Fkmz%2Fwomen_for_women.kmz%3Fa%3Dlarge" 

What is the best way to do this in python?

like image 904
Rolando Avatar asked Aug 22 '12 22:08

Rolando


1 Answers

Python 2's urllib.quote_plus, and Python 3's urllib.parse.quote_plus

url = "http://earth.google.com/gallery/kmz/women_for_women.kmz?a=large" # Python 2 urllib.quote_plus(url) # Python 3 urllib.parse.quote_plus(url) 

outputs:

'http%3A%2F%2Fearth.google.com%2Fgallery%2Fkmz%2Fwomen_for_women.kmz%3Fa%3Dlarge' 
like image 98
Colin Dunklau Avatar answered Oct 11 '22 04:10

Colin Dunklau