Find current weather of any city using OpenWeathermap API in Python
The OpenWeatherMap is a service that provides weather data, including current weather data, forecasts, and historical data to the developers of web services and mobile applications.
It provides an API with JSON, XML, and HTML endpoints and a limited free usage tier. Making more than 60 calls per minute requires a paid subscription starting at USD 40 per month. Access to historical data requires a subscription starting at 150 USD per month. Users can request current weather information, extended forecasts, and graphical maps (showing cloud cover, wind speed, pressure, and precipitation).
To use this current weather data API, one must need the API key, which can be get from here.
Note: User need to create an account on openweathermap.org then only can use the APIs.
Modules Needed :
Below is the implementation :
Enter city name : Delhi Temperature (in kelvin unit) = 312.15 atmospheric pressure (in hPa unit) = 996 humidity (in percentage) = 40 description = haze
Here in the second approach, we will use some of the following modules and functions as listed below:
1. BeautifulSoup: It is a library in python used to extract data from HTML and XML files i.e for web scraping purposes. It generates a parse tree from the page source code, which can be used to extract data in a more readable and hierarchical manner. For installing a beautiful soup library in the system use the code below in the terminal:-
-->> pip install beautifulsoup
2. Requests: Here we will use Python’s requests module to make HTTP requests. For installing use the code below in the terminal.
-->> pip install requests
3. Here we are using headers because the headers contain protocol-specific information that is placed before the raw message i.e retrieved from the website.
4. After that, we will use the get() function and pass the google search in it along with the name of the city to retrieve the data from google.
Then we will use beautifulsoup and parse the HTML data that is required from the website.
5. Then we will use the select() function to retrieve the particular information like time, info, location, store them in some variable, and, store them further.
After all of this, we will finally print the data i.e stored in the variables.
Enter the Name of City -> Mahoba Searching... Mahoba, Uttar Pradesh Monday, 12:00 am Cloudy 27°C Have a Nice Day:)