Thread: Geocode error

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep Power
    0

    Geocode help!


    So I'm using the Google Geocoding API to obtain the ZIP code when given an address such as 'Lowell Observatory, Flagstaff, AZ' and it returns the zip code of the address as a string '86001'.

    I finally got it to print out the json output, but I'm not sure how to go about getting just the zip code and returning only that and nothing else.

    Here's my code:
    Code:
    def get_zipcode(address):
      zipcode = ''
      # +++your code here+++
      url="http://maps.googleapis.com/maps/api/geocode/json?address=" + urllib.quote(address.encode('utf-8')) + "&sensor=false"
      response = urllib2.urlopen(url).read()
      print response
    Or would it be easier to go the xml path?
    Here's my code for that:
    Code:
    def get_zipcode(address):
      zipcode = ''
      # +++your code here+++
      url="http://maps.googleapis.com/maps/api/geocode/xml?address=" + urllib.quote(address.encode('utf-8')) + "&sensor=false"
      response = urllib2.urlopen(url).read()
      tree = ET.parse(response)??
      root = tree.getroot()??
      return root
    For the xml code, it's kind of weird b/c I get an error (IOError: [Errno 2] No such file or directory) but it still returns the xml output...

    If someone could show me how to go about it either way or both, I would really appreciate it
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    This should give you a hint ...
    Code:
    ''' jason_decode2dict1.py
    extracting data from a json object
    tested with Python273
    '''
    
    import urllib
    import urllib2
    import json
    
    address = "Lowell Observatory, Flagstaff, AZ"
    url1 = "http://maps.googleapis.com/maps/api/geocode/json?address="
    url2 = url1 + urllib.quote(address.encode('utf-8')) + "&sensor=false"
    
    # this will be a json string object
    response = urllib2.urlopen(url2).read()
    
    print(type(response))  # <type 'str'> 
    
    # decode json string object to Python dictionary object
    jdecode = json.JSONDecoder()
    response2 = jdecode.decode(response)
    
    print(type(response2))  # <type 'dict'> 
    
    # fish out the address
    address = response2['results'][0]['formatted_address']
    print(address)
    
    ''' result ...
    Lowell Observatory, 1400 West Mars Hill Road, Flagstaff, AZ 86001, USA
    '''
    
    # now fish out the zipcode
    print('-'*40)
    address_list = address.split(',')
    print(address_list)
    print(address_list[-2])
    print(address_list[-2].split())
    # finally ...
    zipcode = address_list[-2].split()[1]
    print(zipcode)
    
    ''' result ...
    [u'Lowell Observatory', u' 1400 West Mars Hill Road', u' Flagstaff', u' AZ 86001', u' USA']
     AZ 86001
    [u'AZ', u'86001']
    86001
    '''
    Last edited by Dietrich; February 9th, 2013 at 06:02 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep Power
    0
    I'm getting an error on the jdecode part for some reason.
    ValueError: No JSON object could be decoded.

    Also, after looking through the json code and whatnot, I think xml output might be easier for me? At least to grasp better - so I tried it out that way, and got it to print out what I want.

    Thanks!
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Looks like xml is a little easier in your case ...
    Code:
    ''' xml_get_data2.py
    extracting data from a xml object
    tested with Python273
    '''
    
    import urllib
    import urllib2
    import xml.etree.ElementTree as ET
    
    #address = "Lowell Observatory, Flagstaff, AZ"
    address = "Disneyland, Anaheim, CA"
    url1 = "http://maps.googleapis.com/maps/api/geocode/xml?address="
    url2 = url1 + urllib.quote(address.encode('utf-8')) + "&sensor=false"
    
    # this will be a xml string object
    xml_response = urllib2.urlopen(url2).read()
    
    root = ET.fromstring(xml_response)
    comp_list = [code for code in root.findall(".//address_component") \
         if code.findtext('type') == 'postal_code']
    zipcode = [element.find('long_name').text for element in comp_list]
    
    print(zipcode)
    print(zipcode[0])
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    27
    Rep Power
    0
    Yep! Thanks for all your help Dietrich!

IMN logo majestic logo threadwatch logo seochat tools logo