0

I'm trying to create a program that can successfully tell you the weather via using a weather api. This is the api I will be using http://www.wunderground.com/weather/api/d/docs

I'm struggling to understand how to use this Api. I'm finding it to be rather confusing. I have tried to use the sample code presented by wunderground however it doesn't seem to be working in my editor(possibility due to code being another version of python.) I'm using python 3.5 Any comments and any suggestions would be greatly appreciated on how I can go about using this api.

Thanks

1 Answers1

0

Here is the sample code modified for Python 3:

from urllib.request import Request, urlopen
import json

API_KEY = 'your API key'
url = 'http://api.wunderground.com/api/{}/geolookup/conditions/q/IA/Cedar_Rapids.json'.format(API_KEY)

request = Request(url)
response = urlopen(request)
json_string = response.read().decode('utf8')
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
print("Current temperature in %s is: %s" % (location, temp_f))
response.close()

Obviously you need to sign up and get an API key. Use that key as the value for API_KEY. If you look at the code sample whilst logged in the key will already be inserted into the URL for you.

You could also use the requests module which is easier to work with, and supports Python 2 and 3:

import requests

API_KEY = 'your API key'
url = 'http://api.wunderground.com/api/{}/geolookup/conditions/q/IA/Cedar_Rapids.json'.format(API_KEY)

response = requests.get(url)
parsed_json = response.json()
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
print("Current temperature in %s is: %s" % (location, temp_f))
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • thanks for this sample code however when I put the code in idle I get this error. location = parsed_json['location']['city'] KeyError: 'location' – user3541130 Jan 24 '16 at 09:06
  • Why do you think that might be? Try looking at the value of `parsed_json`. You might find that the API key that you used is incorrect. Or perhaps the city that you've requested is invalid. Try `parsed_json['response']['error']` – mhawke Jan 24 '16 at 10:55
  • Again, look at the value of `parsed_json`? You are now reporting a different error - does `parsed_json['location']['city']` work? Did you sign up and retrieve an API key? This is pretty basic stuff. Perhaps you need to read up on Python dictionaries. – mhawke Jan 24 '16 at 12:49
  • no it doesn't work. parsed_json['location']['city'] and yes I have got a key – user3541130 Jan 24 '16 at 12:51
  • So what _exactly_ is contained in `parsed_json`? Take a look at it and post it here if it is not too long. You probably have an error response. – mhawke Jan 24 '16 at 12:53
  • this is the problem File "C:/Users/Muzz/Documents/Python Project/weatherlocationwithapi.py", line 16, in temp_f = parsed_json['current_observation']['temp_f'] KeyError: 'current_observation – user3541130 Jan 24 '16 at 12:55
  • I know that. Final try: what is the value of `parsed_json`? Do not try to access its keys, just display it with `print(parsed_json)`. – mhawke Jan 24 '16 at 12:57
  • This is what happens {'response': {'error': {'description': 'this key does not exist', 'type': 'keynotfound'}, 'features': {}, 'termsofService': 'http://www.wunderground.com/weather/api/d/terms.html', 'version': '0.1'}} – user3541130 Jan 24 '16 at 13:02
  • OK, so you are not using a valid API key. Did you replace the line in the sample script `API_KEY = 'your API key'` with _your_ API key? It should be a 16 byte hex string, e.g. `API_KEY = 'a17e6a58ed4001a9'` – mhawke Jan 24 '16 at 13:06
  • yes @mhawke API_KEY = 'Your key here' - i placed my api key in here – user3541130 Jan 24 '16 at 13:19
  • Then you have used the wrong key. You can try with my key which I created just for testing : `API_KEY = 'f17e6a58ed4001a9'`. – mhawke Jan 24 '16 at 13:32
  • @mkhawke thanks a lot this works, i dont understand how i've got the wrong key. I signed up and selected the developer version however the API key that I used was incorrect. I received my key in the table where it gives the url referral link . – user3541130 Jan 24 '16 at 13:41