Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Accessing Weather Data | Exploring Public Data APIs
Working with APIs in Python

bookAccessing Weather Data

The Open-Meteo API is a free and easy-to-use service that allows you to access weather forecasts and current weather data without needing to register or obtain an API key. You can retrieve weather data by simply specifying the geographic coordinates (latitude and longitude) of your location. This makes the API ideal for quick experiments and learning how to work with real-world weather data in Python.

12345678910111213141516171819
import requests # Set the coordinates for New York City latitude = 40.7128 longitude = -74.0060 # Prepare the API endpoint and parameters url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": latitude, "longitude": longitude, "current_weather": True } # Make the GET request to the Open-Meteo API response = requests.get(url, params=params) # Print the JSON response print(response.json())
copy

In the code above, you use three key parameters to request weather data from the Open-Meteo API:

  • Latitude: specifies the north-south position of your location;
  • Longitude: specifies the east-west position of your location;
  • Current_weather: set to True to request the current weather data for the given coordinates.

These parameters allow you to pinpoint any location on the globe and retrieve up-to-date weather information for that spot.

123456789101112131415161718192021
import requests latitude = 40.7128 longitude = -74.0060 url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": latitude, "longitude": longitude, "current_weather": True } response = requests.get(url, params=params) data = response.json() # Extract temperature and wind speed from the current weather data temperature = data["current_weather"]["temperature"] wind_speed = data["current_weather"]["windspeed"] print("Temperature:", temperature, "Β°C") print("Wind speed:", wind_speed, "km/h")
copy

When interpreting weather data fields from the API response, it is important to pay attention to the units. For example, temperature is provided in degrees Celsius (Β°C), while wind speed is given in kilometers per hour (km/h). Understanding these units ensures that you can present and analyze the weather data correctly. Other fields, such as wind direction, are typically expressed in degrees, where 0Β° means north, 90Β° is east, 180Β° is south, and 270Β° is west.

1. Which parameter is required to specify the location in the Open-Meteo API?

2. What type of data structure does response.json() return in Python?

question mark

Which parameter is required to specify the location in the Open-Meteo API?

Select the correct answer

question mark

What type of data structure does response.json() return in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to get weather data for a different city?

What other weather parameters can I retrieve from the API?

How do I interpret the wind direction value from the API response?

bookAccessing Weather Data

Swipe to show menu

The Open-Meteo API is a free and easy-to-use service that allows you to access weather forecasts and current weather data without needing to register or obtain an API key. You can retrieve weather data by simply specifying the geographic coordinates (latitude and longitude) of your location. This makes the API ideal for quick experiments and learning how to work with real-world weather data in Python.

12345678910111213141516171819
import requests # Set the coordinates for New York City latitude = 40.7128 longitude = -74.0060 # Prepare the API endpoint and parameters url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": latitude, "longitude": longitude, "current_weather": True } # Make the GET request to the Open-Meteo API response = requests.get(url, params=params) # Print the JSON response print(response.json())
copy

In the code above, you use three key parameters to request weather data from the Open-Meteo API:

  • Latitude: specifies the north-south position of your location;
  • Longitude: specifies the east-west position of your location;
  • Current_weather: set to True to request the current weather data for the given coordinates.

These parameters allow you to pinpoint any location on the globe and retrieve up-to-date weather information for that spot.

123456789101112131415161718192021
import requests latitude = 40.7128 longitude = -74.0060 url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": latitude, "longitude": longitude, "current_weather": True } response = requests.get(url, params=params) data = response.json() # Extract temperature and wind speed from the current weather data temperature = data["current_weather"]["temperature"] wind_speed = data["current_weather"]["windspeed"] print("Temperature:", temperature, "Β°C") print("Wind speed:", wind_speed, "km/h")
copy

When interpreting weather data fields from the API response, it is important to pay attention to the units. For example, temperature is provided in degrees Celsius (Β°C), while wind speed is given in kilometers per hour (km/h). Understanding these units ensures that you can present and analyze the weather data correctly. Other fields, such as wind direction, are typically expressed in degrees, where 0Β° means north, 90Β° is east, 180Β° is south, and 270Β° is west.

1. Which parameter is required to specify the location in the Open-Meteo API?

2. What type of data structure does response.json() return in Python?

question mark

Which parameter is required to specify the location in the Open-Meteo API?

Select the correct answer

question mark

What type of data structure does response.json() return in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt