Stuck with urllib.quote and Unicode/UTF-8

  • Thread starter Marian Steinbach
  • Start date
M

Marian Steinbach

Hi!

I am stuck with calling URLs with parameters containing non-ASCII
characters. I'm creating a url like this.

url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' +
urllib.quote(address) + '&sensor=false&language=DE'

address can be a string like u"Köln, Nordrhein-Westfalen". This results in

http://maps.googleapis.com/maps/api...0Nordrhein-Westfalen&sensor=false&language=DE

and this doesn't seem to meet Google's Encoding expectations. I get an
"INVALID REQUEST" response. (The same things works if there are only
ASCII characters in the address.)

When I manually enter the "Köln"... string in the Firefox address bar,
Firefox makes the URL

http://maps.googleapis.com/maps/api... Nordrhein-Westfalen&sensor=false&language=DE

out of this and it works fine. %C3%B6 seems to be the url-encoded
representation of an UTF-8 representation of a "ö" character.


So the next thing I tried is to get address utf-8-encoded, like this:

url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' +
urllib.quote(address.encode('utf-8')) + '&sensor=false&language=DE'

This gives me an error like

UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position
1: ordinal not in range(128)

Again, the input string for address is u"Köln, Nordrhein-Westfalen".


Can you see what I'm doing wrong or what I would have to to to get
from u"ö" to "%C3%B6"?


Thanks!

Marian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top