Skip to content

Instantly share code, notes, and snippets.

@MBlore
Created September 4, 2022 13:17
Show Gist options
  • Save MBlore/6111e6f3711aa5dc237637a5c9114873 to your computer and use it in GitHub Desktop.
Save MBlore/6111e6f3711aa5dc237637a5c9114873 to your computer and use it in GitHub Desktop.
Loading the weather in Python.
import urllib.request
import json
def loadWeather(city):
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={your API key goes here}&units=imperial'
with urllib.request.urlopen(url) as response:
data = response.read()
result = json.loads(data)
return result
london_weather = loadWeather("London")
singapore_weather = loadWeather("Singapore")
print(f"The temperature in London is:", london_weather["main"]["temp"], "F")
print(f"The temperature in Singapore is:", singapore_weather["main"]["temp"], "F")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment