Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL encoding in python

Is there a simple method I'm missing in urllib or other library for this task? URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits.

Here's an example of an input and my expected output:

Mozilla/5.0 (Linux; U; Android 4.0; xx-xx; Galaxy Nexus Build/IFL10C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30  Mozilla%2F5.0+%28Linux%3B+U%3B+Android+4.0%3B+xx-xx%3B+Galaxy+Nexus+Build%2FIFL10C%29+AppleWebKit%2F534.30+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Mobile+Safari%2F534.30 
like image 266
wim Avatar asked Jan 18 '12 06:01

wim


People also ask

How do you encode a URL in Python?

In Python 3+, You can URL encode any string using the quote() function provided by urllib. parse package. The quote() function by default uses UTF-8 encoding scheme.

How do I remove 20 from a URL in Python?

replace('%20+', '') will replace '%20+' with empty string.

How do you convert a URL to a string in Python?

Use the urllib. parse. urlencode() function (with the doseq parameter set to True ) to convert such dictionaries into query strings.


1 Answers

For Python 2.x, use urllib.quote

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 '/'.

example:

In [1]: import urllib  In [2]: urllib.quote('%') Out[2]: '%25' 

EDIT:

In your case, in order to replace space by plus signs, you may use urllib.quote_plus

example:

In [4]: urllib.quote_plus('a b') Out[4]: 'a+b' 

For Python 3.x, use quote

>>> import urllib >>> a = "asdas#@das" >>> urllib.parse.quote(a) 'asdas%23%40das' 

and for string with space use quote_plus

>>> import urllib >>> a = "as da& s#@das" >>> urllib.parse.quote_plus(a) 'as+da%26+s%23%40das' 
like image 92
qiao Avatar answered Oct 02 '22 18:10

qiao