Wunderground API 10 Day Forecast Multiple City - edorlando07/datasciencecoursera GitHub Wiki

import urllib2
import json  

The data is written to a weatherfile.txt file that is located in the same folder that the python code is in.

f2 = open("weatherfile.txt","w")  

Your own api_key is needed in order to run the code. It can be retrieved by going to https://www.wunderground.com | Weather API.

api_key = "cb8d75#########"

There is a list of the 10 day forecast listed in the json file. The nested loop listed below includes this day_range.

day_range = range(10)  

The free version allows up to 10 queries per minute. Below is a city_urls list that will be used to loop through the various cities listed.

city_urls = [
        "http://api.wunderground.com/api/" + api_key + "/forecast10day/q/IL/Chicago.json",
        "http://api.wunderground.com/api/" + api_key + "/forecast10day/q/TN/Nashville.json"
        ]  

The code below sets up the various titles (pipe delimited) that will be printed in the weather.txt file.

titles = "state|city|pretty_n|epoch_n|year_n|month_n|day_n|period_n|high_n|low_n|conditions_n|qpf_allday_n|
         snow_allday_n|avewind_n|avehumidity_n" + "\n"

print titles
f2.write(titles)  

The first for loop below will do the following: Select the first url in city_urls | parse out the state and the city text in the url. Those variables are then used in the data file.

for url_ in city_urls:

    state_beg = "/q/"
    city_end = ".json"

    state_beg_int = url_.find(state_beg) + 3
    state_end_int = url_.find(state_beg) + 5

    city_beg_int = url_.find(state_beg) + 6
    city_end_int = url_.find(city_end)  

The code below opens up the urls in the city_urls list (one at a time) | converts it into a json_string so that it can be read and parsed.

    f = urllib2.urlopen(url_)
    json_string = f.read()
    parsed_json = json.loads(json_string)  

The nested loop below goes through the list of the 10 days listed in the 10 day forecast. Some of the variables listed for each day include temperature, dates, rain percipitation, etc.

    for date_n in day_range:
        pretty_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['date']['pretty']
        epoch_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['date']['epoch']
        year_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['date']['year']
        month_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['date']['month']
        day_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['date']['day']
        period_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['period']
        high_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['high']['fahrenheit']
        low_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['low']['fahrenheit']
        conditions_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['conditions']
        qpf_allday_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['qpf_allday']['in']
        snow_allday_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['snow_allday']['in']
        avewind_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['avewind']['mph']
        avehumidity_n = parsed_json['forecast']['simpleforecast']['forecastday'][date_n]['avehumidity']  

The code below concatenates all the variables listed above into one line for each day for each city (pipe delimited).

        data_line = str(url_[state_beg_int:state_end_int]) +"|" + str(url_[city_beg_int:city_end_int]) + "|" +  
                    str(pretty_n) + "|" + str(epoch_n) + "|" + str(year_n) + "|" + str(month_n) + "|" + str(day_n) +  
                    "|" + str(period_n) + "|" + str(high_n) + "|" + str(low_n) + "|" + str(conditions_n) + "|" + 
                    str(qpf_allday_n) + "|" + str(snow_allday_n) + "|" + str(avewind_n) + "|" + str(avehumidity_n) + 
                    "\n"
    
        print data_line
        f2.writelines(data_line)  

Close the API connection and the txt file that was open.

f.close() 
f2.close()  

###The output for Chicago and Nashville are listed below:

state|city|pretty_n|epoch_n|year_n|month_n|day_n|period_n|high_n|low_n|conditions_n|qpf_allday_n|
snow_allday_n|avewind_n|avehumidity_n

IL|Chicago|7:00 PM CST on February 17, 2017|1487379600|2017|2|17|2|60|42|Clear|0.0|0.0|13|64
IL|Chicago|7:00 PM CST on February 17, 2017|1487379600|2017|2|17|2|60|42|Clear|0.0|0.0|13|64
IL|Chicago|7:00 PM CST on February 18, 2017|1487466000|2017|2|18|3|63|35|Clear|0.0|0.0|10|61
IL|Chicago|7:00 PM CST on February 19, 2017|1487552400|2017|2|19|4|61|43|Clear|0.0|0.0|7|60
IL|Chicago|7:00 PM CST on February 20, 2017|1487638800|2017|2|20|5|64|49|Partly Cloudy|0.03|0.0|13|81
IL|Chicago|7:00 PM CST on February 21, 2017|1487725200|2017|2|21|6|60|43|Chance of Rain|0.02|0.0|10|80
IL|Chicago|7:00 PM CST on February 22, 2017|1487811600|2017|2|22|7|57|40|Clear|0.0|0.0|10|74
IL|Chicago|7:00 PM CST on February 23, 2017|1487898000|2017|2|23|8|52|42|Overcast|0.2|0.0|10|77
IL|Chicago|7:00 PM CST on February 24, 2017|1487984400|2017|2|24|9|57|35|Chance of Rain|0.35|0.0|14|71
IL|Chicago|7:00 PM CST on February 25, 2017|1488070800|2017|2|25|10|39|26|Partly Cloudy|0.0|0.0|22|67
TN|Nashville|7:00 PM CST on February 16, 2017|1487293200|2017|2|16|1|60|41|Clear|0.0|0.0|8|48
TN|Nashville|7:00 PM CST on February 17, 2017|1487379600|2017|2|17|2|69|49|Clear|0.0|0.0|10|45
TN|Nashville|7:00 PM CST on February 18, 2017|1487466000|2017|2|18|3|59|46|Chance of Rain|0.17|0.0|7|70
TN|Nashville|7:00 PM CST on February 19, 2017|1487552400|2017|2|19|4|73|46|Clear|0.0|0.0|4|65
TN|Nashville|7:00 PM CST on February 20, 2017|1487638800|2017|2|20|5|75|54|Partly Cloudy|0.0|0.0|5|64
TN|Nashville|7:00 PM CST on February 21, 2017|1487725200|2017|2|21|6|74|55|Partly Cloudy|0.0|0.0|7|62
TN|Nashville|7:00 PM CST on February 22, 2017|1487811600|2017|2|22|7|70|53|Partly Cloudy|0.0|0.0|5|68
TN|Nashville|7:00 PM CST on February 23, 2017|1487898000|2017|2|23|8|74|55|Partly Cloudy|0.0|0.0|7|65
TN|Nashville|7:00 PM CST on February 24, 2017|1487984400|2017|2|24|9|73|53|Partly Cloudy|0.19|0.0|12|66
TN|Nashville|7:00 PM CST on February 25, 2017|1488070800|2017|2|25|10|60|40|Clear|0.0|0.0|13|55