Python‎ > ‎

Retrieve JSON data from a Web Service

posted Sep 5, 2014, 12:06 AM by Chris G
Python is a great tool for retrieving and processing data. For instance, retrieving the sunset time from the web using the JSON Weather Underground API would only require a few lines of code:
import urllib, json
import datetime


url = "http://api.wunderground.com/api/<your api code>/geolookup/conditions/astronomy/q/CA/fresno.json"
now = datetime.datetime.now()

response = urllib.urlopen(url);
data = json.loads(response.read())
sunSetTime = datetime.datetime(now.year, now.month, now.day, int(data['sun_phase']['sunset']['hour']), int(data['sun_phase']['sunset']['minute']), 00)


Comments