Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python urllib urlencode problem with æøå

How can I urlencode a string with special chars æøå?

ex.

urllib.urlencode('http://www.test.com/q=testæøå')

I get this error :(..

not a valid non-string sequence or mapping object

like image 287
pkdkk Avatar asked Oct 22 '10 12:10

pkdkk


People also ask

What does Urllib parse URL encode do?

parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.

How do you URL encode a string 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.


2 Answers

urlencode is intended to take a dictionary, for example:

>>> q= u'\xe6\xf8\xe5' # u'æøå'
>>> params= {'q': q.encode('utf-8')}
>>> 'http://www.test.com/?'+urllib.urlencode(params)
'http://www.test.com/?q=%C3%A6%C3%B8%C3%A5'

If you just want to URL-encode a single string, the function you're looking for is quote:

>>> 'http://www.test.com/?q='+urllib.quote(q.encode('utf-8'))
'http://www.test.com/?q=%C3%A6%C3%B8%C3%A5'

I'm guessing UTF-8 is the right encoding (it should be, for modern sites). If what you actually want ?q=%E6%F8%E5, then the encoding you want is probably cp1252 (similar to iso-8859-1).

like image 114
bobince Avatar answered Sep 23 '22 13:09

bobince


You should pass dictionary to urlencode, not a string. See the correct example below:

from urllib import urlencode
print 'http://www.test.com/?' + urlencode({'q': 'testæøå'})
like image 39
vorushin Avatar answered Sep 21 '22 13:09

vorushin