Showing posts with label openweathermap. Show all posts
Showing posts with label openweathermap. Show all posts

Tuesday, November 20, 2012

Weather information from openweathermap.org JSON API by Python

The openweathermap.org offers JSON API to query weather information around the world. It is not difficult to use python to call the API, parse the JSON and gather the information.

Since all the request are from web, it'd better to prepare a simple function to fetch the HTML/JSON from the site:

import urllib2, json

def fetchHTML(url):
    req = urllib2.Request(url)
    response=urllib2.urlopen(req)
    return response.read()

   
Remember it is just for a quick and dirty approach. In real case we should handle exceptions (such as URL unreachable, network not found, etc)

Now we are ready to call the API. First of all we need to know the "id" of a City. http://openweathermap.org/data/2.1/find/name?q=<CITY>