I have made a really simple weather app from a tutorial I found online. I'm using the requests module and I'm trying to call the weather for the UK post code GU27PG. I'm using the documentation found at https://openweathermap.org/current#zip to structure my url. Here is my code:
import requests
import configparser
def get_api_key():
config = configparser.ConfigParser()
config.read('config.ini')
return config['openweathermap']['api']
def get_weather_results(zip_code, country_code, api_key):
api_url = 'http://api.openweathermap.org/data/2.5/weather?zip={},{}&appid={}'.format(zip_code, country_code, api_key)
r = requests.get(api_url)
return r.json()
print(get_weather_results("GU27PG", "UK", get_api_key()))
When i run this it is returning this:
{'cod': '404', 'message': 'city not found'}
This is a valid postcode and country code I'm pretty sure. I used to get error 401 which means that my api key isn't activated but that stopped so my api key definitely works. Any help would be greatly appreaciated
The documentation that you linked to is quite vague, so I'm not surprised that you hit a stumbling block. I was about to reply and say that it seemed it wasn't possible to search for UK postcodes using the zip
parameter, but through experimentation I discovered two things:
So in your example, if you ask it to print weather results for GU2,GB
then you will get data back: