Combine API Calls, Trouble with other categories besides 'Current Conditions' (in Python)

  • 1
  • Question
  • Updated 6 years ago
Hi All:

when writing my code for something like current latitude:
import urllib2
import json
f=urllib2.urlopen('http://api.wunderground.com/api/a66b1...)
json_string = f.read()
parsed_json = json.loads(json_string)
location1 = parsed_json ['location']['lat']

I am given the latitude, however when attempting the same with categories such as forecast, tide or astronomy:
sunrise = parsed_json ['sunrise']
print "\nSunrise at: %s" % (sunrise)

I get errors saying sunrise is out of dictionary...is this a wunderground issue or a python issue?
Photo of phluffhed

phluffhed

  • 3 Posts
  • 1 Reply Like

Posted 6 years ago

  • 1
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 123 Reply Likes
It seems there's a missed node in the traversal: "moon_phase"

This should work:
sunrise = parsed_json ['moon_phase']['sunrise']['hour']