Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara 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 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookAccessing Weather Data

Scorri per mostrare il 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 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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1
some-alt