Python provides a few built-in libraries to help with the process of consuming third-party web APIs. In this post I’d like to briefly outline how to retrieve and process web APIs with Python, using the Google Maps Geocoding API as an example.
Here’s a rundown of the libraries we will be using for this demonstration:
urllib2, which defines functions and classes which help in opening URLs
json, which defines helpful functions for dealing with the JSON format
You’ll also want to checkout Requests, an HTTP library with a much cleaner API, written in Python to replace urllib2.
Another library which may be helpful if you are following along is:
pprint, provides a capability to “pretty-print” arbitrary Python data structures, helpful for debugging
Geocoding is the process of converting addresses (like “1600 Amphitheatre Parkway, Mountain View, CA”) into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers or position the map. The Google Geocoding API provides a direct way to access a these services via an HTTP request.
We will interface with the Google Maps Geocoding API and write a method that allows us to geocode addresses. Let’s start by importing all our libraries:
Next, we read from the Google Maps Geocoding API documentation, that a Geocoding API request must be of the following form:
The required parameters for this API are address — the address that you want to geocode — and sensor — which indicates whether or not the geocoding request comes from a device with a location sensor (boolean.) The API provides other optional parameters, such as region, language, and components; however I will leave them out for simplicity.
In this example, the Geocoding API requests a json response for a query on “1600 Amphitheatre Parkway, Mountain View, CA”:
Let’s continue by opening the URL in urllib2:
A few things of note:
We need to escape special characters in strings, such as spaces before we can pass it as a parameter. (#1)
We then make an HTTP GET request with urlopen and save the HTTP response (#2)
Reading the response outputs this mess we get:
Using the pprint library, we can format our response:
Next, let’s deserialize our JSON response (usually a str or unicode instance) to a Python dictionary so we can extract and process it:
Now, it’s just a matter of finding the lat/lng values in the dictionary:
Reorganizing the code into a function, we have:
And we’re done! We now have a method which can geocode address strings into a tuple of latitude/longitude values! You can use them to place markers on a map, calculate their great-circle distance from each other using the haversine formula, or pretty much anything under the sun!
Have you heard about the Serverless programming model? The Going Serverlessbook teaches you how to build scalable applications with the Serverless framework and AWS Lambda. You'll learn how to design, develop, test, deploy, and secure Serverless applications from planning to production.