Stuck with urllib.quote and Unicode/UTF-8

Discussion in 'Python' started by Marian Steinbach, May 7, 2011.

  1. 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
     
    Marian Steinbach, May 7, 2011
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Nagle

    urllib.quote fails on Unicode URL

    John Nagle, May 4, 2007, in forum: Python
    Replies:
    1
    Views:
    512
    Peter Otten
    May 4, 2007
  2. Valery Khamenya
    Replies:
    3
    Views:
    745
  3. Jerry Hill
    Replies:
    0
    Views:
    500
    Jerry Hill
    Oct 4, 2008
  4. Evan
    Replies:
    1
    Views:
    421
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. Grzegorz ¦liwiñski
    Replies:
    2
    Views:
    1,027
    Grzegorz ¦liwiñski
    Jan 19, 2011
Loading...

Share This Page