Python 2: Print 10-Day Forecast Parsed JSON Data from Weather Underground API

urllib2.urlopen, json.read, json.load

nick3499
3 min readJan 17, 2018

Weather Underground API provides weather data. An API key is required.

urllib2.urlopen returns an object which contains 3 methods: geturl(), info() and getcode(). geturl() gets the data from the API, info() handles meta info and getcode() is for GET request status.

p['forecast']['txt_forecast']['forecastday'] returns a list of 20 parsed JSON forecast dictionaries, for daytime and nighttime forecasts. But unavailable data is simply omitted, so that total could change, which explains why the for loop range is based on the inconsistent len() of forecastday. title key name signifies a string for the name of the day of the week, including night. fcttextsignifies a string which describes the forecast. pop signifies an integer for probability of precipitation.

Weather Forecast: 10 Day (Weather Underground API)
Time: 11:34 PM CST
Wednesday
* forecast: Mostly sunny skies. High 22F. Winds WSW at 10 to 20 mph.
* precip. prob.: 10%
Wednesday Night
* forecast: Clear skies. Low 16F. Winds SW at 15 to 25 mph.
* precip. prob.: 0%
Thursday
* forecast: Sunny skies. High 33F. Winds SW at 10 to 20 mph.
* precip. prob.: 0%
Thursday Night
* forecast: Mainly clear early, then a few clouds later on. Low 22F. Winds SW at 10 to 15 mph.
* precip. prob.: 10%
Friday
* forecast: Except for a few afternoon clouds, mainly sunny. High 39F. Winds SSW at 10 to 20 mph.
* precip. prob.: 10%
Friday Night
* forecast: Partly cloudy skies. Low 28F. Winds SSW at 10 to 20 mph.
* precip. prob.: 10%
Saturday
* forecast: Partly cloudy skies in the morning will give way to cloudy skies during the afternoon. High 42F. Winds SSW at 10 to 15 mph.
* precip. prob.: 20%
Saturday Night
* forecast: Cloudy with occasional showers. Low 32F. Winds S at 5 to 10 mph. Chance of rain 40%.
* precip. prob.: 40%
Sunday
* forecast: Showers early becoming a steady light rain later in the day. High 46F. Winds SSE at 10 to 15 mph. Chance of rain 70%.
* precip. prob.: 70%
Sunday Night
* forecast: Periods of rain. Low 38F. Winds S at 10 to 20 mph. Chance of rain 90%. Rainfall around a half an inch.
* precip. prob.: 90%
Monday
* forecast: Rain...mixing with snow in the afternoon. High 41F. Winds SW at 15 to 25 mph. Chance of rain 80%.
* precip. prob.: 80%
Monday Night
* forecast: Mainly cloudy with snow showers around before midnight. Low 24F. Winds WSW at 15 to 25 mph. Chance of snow 40%.
* precip. prob.: 40%
Tuesday
* forecast: Cloudy skies early will become partly cloudy later in the day. High 42F. Winds WSW at 10 to 15 mph.
* precip. prob.: 20%
Tuesday Night
* forecast: A few clouds from time to time. Low near 25F. Winds WSW at 5 to 10 mph.
* precip. prob.: 20%
Wednesday
* forecast: Mostly cloudy skies. High 38F. Winds WNW at 5 to 10 mph.
* precip. prob.: 10%
Wednesday Night
* forecast: On and off snow showers early. Breaks in the overcast later. Low 22F. Winds NW at 5 to 10 mph. Chance of snow 30%.
* precip. prob.: 30%
Thursday
* forecast: Sunshine along with some cloudy intervals. High 33F. Winds WNW at 5 to 10 mph.
* precip. prob.: 10%
Thursday Night
* forecast: Partly cloudy skies during the evening will give way to cloudy skies overnight. Low 21F. Winds SSE at 5 to 10 mph.
* precip. prob.: 10%
Friday
* forecast: Considerable cloudiness. High 34F. Winds SE at 5 to 10 mph.
* precip. prob.: 20%
Friday Night
* forecast: Mostly cloudy. Snow showers around after midnight. Low 22F. Winds SSE at 5 to 10 mph. Chance of snow 50%. About one inch of snow expected.
* precip. prob.: 50%

--

--

nick3499
nick3499

Written by nick3499

coder of JavaScript and Python

No responses yet