Skip to content

Instantly share code, notes, and snippets.

@FrankRuns
Created December 16, 2018 14:46
Show Gist options
  • Save FrankRuns/2086a4a5bc9b8c9787d4ddd0453ae971 to your computer and use it in GitHub Desktop.
Save FrankRuns/2086a4a5bc9b8c9787d4ddd0453ae971 to your computer and use it in GitHub Desktop.
# Weather Underground API history call format example
# http://api.wunderground.com/api/<YOUR_API_KEY>/history_20060405/q/CA/San_Francisco.json
start_string = "http://api.wunderground.com/api/<YOUR_API_KEY>/history_"
end_string = "/q/MN/Minneapolis.json"
the_dates = ['2015-05-24', '2015-10-29', '2015-11-06', '2015-09-27',
'2015-04-01', '2015-06-16', '2015-08-28', '2015-08-30',
'2015-05-15', '2015-03-06', '2015-01-24', '2015-05-07',
'2015-01-02', '2015-09-01', '2015-10-17', '2015-08-10',
'2015-05-23', '2015-11-04', '2015-09-14', '2015-02-08',
'2015-11-27', '2015-02-13', '2015-11-03', '2015-10-28',
'2015-07-17', '2015-01-26', '2015-09-07', '2015-02-01',
'2015-02-20', '2015-01-18', '2015-06-24', '2015-04-08',
'2015-05-17', '2015-04-20', '2015-02-21', '2015-06-28',
'2015-08-24', '2015-09-29', '2015-04-18', '2015-01-22',
'2015-03-02', '2015-03-31', '2015-11-24', '2015-01-29',
'2015-03-20', '2015-09-20', '2015-06-01', '2015-01-31',
'2015-10-10', '2015-07-29', '2015-01-10', '2015-04-04',
'2015-06-27', '2015-10-01', '2015-07-25', '2015-06-13',
'2015-05-27', '2015-06-10', '2015-02-03', '2015-01-12',
'2015-01-14', '2015-06-03', '2015-03-21', '2015-09-15',
'2015-08-20', '2015-09-04', '2015-10-21', '2015-04-24',
'2015-02-19', '2015-12-27', '2015-10-08', '2015-11-07',
'2015-11-23', '2015-01-06', '2015-06-11', '2015-09-21',
'2015-02-06', '2015-04-12', '2015-04-26', '2015-07-19',
'2015-07-16', '2015-08-04', '2015-09-16', '2015-10-27',
'2015-05-30', '2015-03-09', '2015-02-12', '2015-11-22',
'2015-08-05', '2015-07-02', '2015-09-25', '2015-04-09',
'2015-05-04', '2015-11-14', '2015-07-23', '2015-03-19',
'2015-04-28', '2015-02-22', '2015-05-19', '2015-03-26',
'2015-03-11', '2015-09-23', '2015-08-22', '2015-03-30',
'2015-12-18', '2015-11-11', '2015-08-26', '2015-06-15',
'2015-10-22', '2015-11-26', '2015-05-14', '2015-01-13',
'2015-11-15', '2015-02-07', '2015-11-28', '2015-05-06',
'2015-02-28', '2015-09-12', '2015-03-04', '2015-03-07',
'2015-04-22', '2015-09-11', '2015-06-18', '2015-08-29',
'2015-12-06', '2015-03-13', '2015-05-08', '2015-12-25',
'2015-01-20', '2015-07-13', '2015-09-02', '2015-09-28',
'2015-03-24', '2015-11-19', '2015-10-18', '2015-06-19',
'2015-12-09', '2015-08-16', '2015-10-02', '2015-01-16',
'2015-06-02', '2015-08-18', '2015-04-30', '2015-07-27',
'2015-08-11', '2015-04-17', '2015-07-01', '2015-11-29',
'2015-12-04', '2015-01-30', '2015-06-06', '2015-08-15',
'2015-10-06', '2015-06-20', '2015-12-30', '2015-10-24',
'2015-07-12', '2015-12-01', '2015-05-03', '2015-11-17',
'2015-02-18', '2015-05-01', '2015-03-29', '2015-10-30',
'2015-02-14', '2015-06-22', '2015-06-07', '2015-10-25',
'2015-09-05', '2015-04-11', '2015-10-23', '2015-07-31',
'2015-04-07', '2015-05-31', '2015-11-09', '2015-04-05',
'2015-08-12', '2015-04-23', '2015-01-09', '2015-09-09',
'2015-05-20', '2015-03-18', '2015-06-25', '2015-01-01',
'2015-09-08', '2015-07-20', '2015-07-24', '2015-07-07',
'2015-04-13', '2015-12-03', '2015-08-31', '2015-10-11',
'2015-12-08', '2015-04-03', '2015-07-10', '2015-11-18',
'2015-04-21', '2015-10-09', '2015-01-04', '2015-03-15',
'2015-02-24', '2015-05-16', '2015-07-09', '2015-11-16',
'2015-01-28', '2015-05-09', '2015-12-20', '2015-07-11',
'2015-03-28', '2015-02-09', '2015-11-08', '2015-12-05',
'2015-01-27', '2015-03-01', '2015-06-05', '2015-02-25',
'2015-04-29', '2015-01-17', '2015-01-11', '2015-02-11',
'2015-07-05', '2015-08-14', '2015-03-23', '2015-02-10',
'2015-09-13', '2015-12-24', '2015-12-07', '2015-12-17',
'2015-12-21', '2015-06-17', '2015-12-23', '2015-12-02',
'2015-09-24', '2015-08-23', '2015-09-06', '2015-04-19',
'2015-10-31', '2015-03-14', '2015-03-03', '2015-07-18',
'2015-12-14', '2015-06-26', '2015-06-23', '2015-12-31',
'2015-04-27', '2015-02-15', '2015-04-10', '2015-08-01',
'2015-09-17', '2015-09-18', '2015-11-01', '2015-10-13',
'2015-07-06', '2015-03-27', '2015-07-04', '2015-08-09',
'2015-12-29', '2015-10-12', '2015-07-08', '2015-01-23',
'2015-12-15', '2015-12-22', '2015-05-29', '2015-01-07',
'2015-11-12', '2015-06-09', '2015-11-21', '2015-02-26',
'2015-05-05', '2015-07-15', '2015-12-13', '2015-06-08',
'2015-06-30', '2015-12-26', '2015-12-12', '2015-08-21',
'2015-05-02', '2015-11-02', '2015-03-17', '2015-11-10',
'2015-11-20', '2015-09-03', '2015-01-05', '2015-04-14',
'2015-04-02', '2015-12-11', '2015-10-16', '2015-07-30',
'2015-01-15', '2015-05-18', '2015-05-22', '2015-11-13',
'2015-08-19', '2015-07-03', '2015-11-30', '2015-06-04',
'2015-02-27', '2015-10-03', '2015-08-07', '2015-10-14',
'2015-07-14', '2015-04-16', '2015-08-27', '2015-09-30',
'2015-06-21', '2015-03-12', '2015-07-22', '2015-02-05',
'2015-07-28', '2015-04-15', '2015-05-28', '2015-05-11',
'2015-08-17', '2015-05-26', '2015-01-19', '2015-02-02',
'2015-10-26', '2015-03-05', '2015-02-04', '2015-03-10',
'2015-11-25', '2015-10-05', '2015-03-25', '2015-04-25',
'2015-05-25', '2015-02-16', '2015-10-19', '2015-07-26',
'2015-08-02', '2015-05-10', '2015-02-17', '2015-05-13',
'2015-01-25', '2015-12-19', '2015-09-26', '2015-02-23',
'2015-01-08', '2015-12-10', '2015-09-19', '2015-10-07',
'2015-06-29', '2015-10-20', '2015-10-04', '2015-01-21', '2015-05-12']
import pandas as pd
import requests
import json
import time
weather_data = pd.DataFrame(data = {'date': [], 'conds': [], 'fog': [],
'hail': [], 'rain': [], 'snow': [], 'tempi': [], 'visi': []})
columnsTitles = ['date', 'conds', 'fog', 'hail', 'rain', 'snow', 'tempi', 'visi']
weather_data = weather_data.reindex(columns=columnsTitles)
count = 0
for el in the_dates:
formatted_date = el.replace('-', '')
api_string = start_string + formatted_date + end_string
print api_string
r = requests.get(api_string)
tempdata = r.json()
conds = tempdata['history']['observations'][8]['conds']
fog = tempdata['history']['observations'][8]['fog']
hail = tempdata['history']['observations'][8]['hail']
rain = tempdata['history']['observations'][8]['rain']
snow = tempdata['history']['observations'][8]['snow']
tempi = tempdata['history']['observations'][8]['tempi']
visi = tempdata['history']['observations'][8]['visi']
tempdf = pd.DataFrame([[el, conds, fog, hail, rain, snow, tempi, visi]],
columns=columnsTitles)
weather_data = weather_data.append(tempdf)
print('added date = ' + el)
count+=1
if count % 10 == 0:
time.sleep(60)
weather_data.to_csv('weather_data.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment