Junior — Middle
Have you had experience interacting with APIs for weather forecast retrieval?
sobes.tech AI
Answer from AI
Yes, I have worked with APIs for obtaining weather forecasts, such as OpenWeatherMap.
The process usually looks like this:
- Register and get an API key.
- Send an HTTP request with parameters (city, coordinates, forecast type).
- Receive a JSON response with data on temperature, humidity, wind, etc.
Here's a simple example of a request in Python using the requests library:
import requests
api_key = 'your_api_key'
city = 'Moscow'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(f"Temperature in {city}: {data['main']['temp']}°C")
else:
print("Error fetching data")
This approach allows integrating weather data into applications for displaying forecasts or making decisions.