Get your weather with the OpenWeatherMap
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
In this lesson we are going to learn how to use the Open Weather API to get your local weather.
First, let's make sure you have the necessary libraries installed. You will need requests and json. You can install them using pip like this:
pip install requests
pip install json
Next, we need to get an API key from Open Weather. Go to their website and sign up for an account. Once you have an account, you can find your API key on the dashboard.
Now that we have our API key, we can start making requests to the API. First, let's import the necessary libraries at the top of our script:
import requests
import json
Next, let's create a function that will make the request to the API and return the weather data. We will pass the function our API key and our location as parameters.
# Create function to make API call and return data
def get_weather(api_key, zip, country):
url = "https://api.openweathermap.org/data/2.5/weather"
query_params = {
"zip": f"{zip},{country}", # Zip, Country
"appid": api_key,
"units": 'imperial' # Metric, Standard or Imperial
}
response = requests.get(url, params=query_params)
response_json = json.loads(response.text)
return response_json
Now let's call our function and print out the weather data.
api_key = "YOUR_API_KEY_HERE"
location = "San Francisco, CA"
weather_data = get_weather(api_key, location)
print(weather_data)
You should see a bunch of data printed out on the screen, including the current temperature, humidity, and wind speed.
To access specific data points, you can use the get() method on the weather_data object. For example, to get the current temperature, you can use:
temperature = weather_data.get('main').get('temp')
print(temperature)
Try playing around with the data and see what other information you can access!
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Python Lists
Text | 8 min
Python Tuples
Text | 7 min
Python Sets
Text | 7 min
Frozensets
Text | 6 min
Dictionaries
Text | 8 min
Iterator and Iterable
Text | 5 min
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Section Overview
Text | 4 min
Regex 101
Text | 10 min
Importing Regex and manipulating strings
Text | 7 min
Regex Challenge!
Text | 5 min
Working with APIs • 12min
0 / 3 lessons complete
Course Conclusion • 2min
0 / 1 lessons complete