Welcome to the Weather CLI (API) page.
This page contains a brief description and implementation instructions for the project.
Project Description
A command-line program that fetches current weather data for a given city using a web API (e.g. OpenWeatherMap). The user enters a city name, and the program displays temperature, humidity, conditions, etc. For example, one GfG solution shows how to use requests
to get JSON from OpenWeatherMap.
Project Requirements
Python 3.x, requests
library (install via pip
), and an API key for a weather service like OpenWeatherMap.
How to Run the Project
- Register with a weather API (e.g. OpenWeatherMap) to get an API key.
- Write a script (e.g.
weather.py
) that usesrequests.get()
to call the API endpoint and parses the JSON response. - Run it:
python weather.py
. Enter a city when prompted; the program outputs weather details.
Key Concepts Learned
- APIs and HTTP: Using
requests
to perform an HTTP GET request to an API endpoint. - JSON Parsing: Converting the response into a Python dict and extracting data (e.g.
response.json()["main"]["temp"]
). - String Formatting: Displaying the weather information clearly.
- Error Handling: Checking for invalid city names and API failures (GfG handles “City Not Found”).
Possible Extensions
- Provide forecasts or weather for multiple cities.
- Add command-line arguments or flags (e.g.
--units metric
). - Cache results to avoid repeated API calls.
- Implement a graphical version (web or GUI).