Using Google Maps geocoder from Python with urllib2

前端 未结 1 1956
小鲜肉
小鲜肉 2020-12-19 21:23

I\'m trying to use the Google Maps geocoder with Python and JSON, but keep being told I have a bad request:

add = \"Buckingham Palace, London, SW1A 1AA\"
ge         


        
相关标签:
1条回答
  • 2020-12-19 22:12

    You need some URL quoting encoding, this works:

    import urllib2
    import pprint
    import json
    add = "Buckingham Palace, London, SW1A 1AA"
    add = urllib2.quote(add)
    geocode_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false&region=uk" % add
    print geocode_url
    req = urllib2.urlopen(geocode_url)
    jsonResponse = json.loads(req.read())
    pprint.pprint(jsonResponse) 
    

    The line add = urllib2.quote(add) is the important point. If you have non latin characters be advised that the google API needs UTF-8 encoding.

    0 讨论(0)
提交回复
热议问题