7 Replies - 749 Views - Last Post: 09 February 2013 - 03:21 AM Rate Topic: -----

#1 donkatsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-February 13

Python Data Mashup - geocode

Posted 08 February 2013 - 03:27 PM

Hi, I'm very new to python and am still learning so please bare with me.

I have this code:
import urllib, urllib2, re
import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup

# The get_zipcode(address) function will be called with an address string with
# no zip code, such as 'Lowell Observatory, Flagstaff, AZ', and it should return the
# ZIP code of the address as a string
# For example, get_zipcode('Lowell Observatory, Flagstaff, AZ') should return '86001'
# Use the Google Geocoding API to obtain the ZIP code. Documentation:
# https://developers.google.com/maps/documentation/geocoding/
# Can use either json or xml output format. 
def get_zipcode(address):
  zipcode = ''
  # +++your code here+++
  url_values = urllib.urlencode(address)
  url="http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" % address
  #return zipcode
  response = urllib2.urlopen(url)
  return response


I'm getting an error when I return response and I don't know why. Also, I'm not exactly if I am going about this the right way at all.

Is This A Good Question/Topic? 0
  • +

Replies To: Python Data Mashup - geocode

#2 bgammill  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 39
  • Joined: 11-June 12

Re: Python Data Mashup - geocode

Posted 08 February 2013 - 03:49 PM

What error are you receiving.
Was This Post Helpful? 0
  • +
  • -

#3 donkatsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-February 13

Re: Python Data Mashup - geocode

Posted 08 February 2013 - 03:55 PM

With this code:
def get_zipcode(address):
  zipcode = ''
  # +++your code here+++
  url="http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" % address
  #return zipcode
  response = urllib2.urlopen(url)
  return response


I'm getting an urllib2.HTTPError: HTTP 400 Bad request.
I'm not really sure if I'm doing this right either but I think I'm in the right direction?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: Python Data Mashup - geocode

Posted 08 February 2013 - 04:10 PM

I dimly recall %s having problems if followed immediately by a non-whitespace character. Might be worth a shot with curly brackets:

url="http://maps.googleapis.com/maps/api/geocode/json?address={%s}&sensor=false" % address

Was This Post Helpful? 0
  • +
  • -

#5 donkatsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-February 13

Re: Python Data Mashup - geocode

Posted 08 February 2013 - 04:18 PM

View Postandrewsw, on 08 February 2013 - 04:10 PM, said:

I dimly recall %s having problems if followed immediately by a non-whitespace character. Might be worth a shot with curly brackets:

url="http://maps.googleapis.com/maps/api/geocode/json?address={%s}&sensor=false" % address


Hmm, I tried that, but still got the same error. Perhaps there's something I'm missing?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: Python Data Mashup - geocode

Posted 08 February 2013 - 04:24 PM

print() the address and url, and test the url+address combination by typing it in your browsers address-bar.

urlencode the address before inserting it into the url.
Was This Post Helpful? 0
  • +
  • -

#7 donkatsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-February 13

Re: Python Data Mashup - geocode

Posted 08 February 2013 - 04:32 PM

Now I get this weird error: not a valid non-string sequence or mappying object...
It works in my address bar but not when I return it which is really strange.

Here's what I have following your advice.

def get_zipcode(address):
zipcode = ''
# +++your code here+++
url_values = urllib.urlencode(address)
url="http://maps.googleapis.com/maps/api/geocode/json?address={%s}&sensor=false" % url_values
print url_values
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,527
  • Joined: 12-December 12

Re: Python Data Mashup - geocode

Posted 09 February 2013 - 03:21 AM

I suggested that you try the curly brackets; as it didn't resolve the issue you could remove them for the moment.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1