Wunderground API: Multiple City: Today's Forecast - edorlando07/datasciencecoursera GitHub Wiki

###The code below lists today's forecast for multiple cities.
Misc Note: Python dictionaries = JSON objects | Python lists or tuples = JSON array

import urllib2
import json
import datetime  

Below is city_urls that is a list of all the various city urls.
This list will be looped through to provide various city weather stats.

city_urls = [
            'http://api.wunderground.com/api/cb8d75#####/geolookup/conditions/q/IL/Chicago.json',
            'http://api.wunderground.com/api/cb8d75#####/geolookup/conditions/q/IN/Indianapolis.json',
            'http://api.wunderground.com/api/cb8d75#####/geolookup/conditions/q/TN/Nashville.json',
            'http://api.wunderground.com/api/cb8d75#####/geolookup/conditions/q/TX/Austin.json',
            'http://api.wunderground.com/api/cb8d75#####/geolookup/conditions/q/TX/Houston.json',
            'http://api.wunderground.com/api/cb8d75#####/geolookup/conditions/q/CO/Denver.json'
            ]  

The print statement below prints the titles for each of the records to be printed later.

print   "Location|" + "Temperature|" + "Feels like|" + "Weather|" + "Wind in mph|" + "Precip Today Inches|" + "Date"  

The loop will open up the various sites listed above and pull the data listed within the loop (city, temp, weather, etc)

for url_ in city_urls:
    f = urllib2.urlopen(url_)
    json_string = f.read()

    parsed_json = json.loads(json_string)

    location = parsed_json['location']['city']
    temp_f = parsed_json['current_observation']['temp_f']
    feelslike_f = parsed_json['current_observation']['feelslike_f']
    weather = parsed_json['current_observation']['weather']
    wind_mph = parsed_json['current_observation']['wind_mph']
    precip_today_in = parsed_json['current_observation']['precip_today_in']
    today = datetime.date.today()

    print   str(location) + "|" + str(temp_f) + "|" + str(feelslike_f) + "|" + str(weather) + "|" + str(wind_mph) +  
    "|" + str(precip_today_in) + "|" + str(today)

f.close()

###Listed below is some documentation regarding json.loads json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]]) Deserialize s (a str or unicode instance containing a JSON document) to a Python object using this conversion table.

If s is a str instance and is encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to unicode first.

The other arguments have the same meaning as in load().