Javatpoint Logo
Javatpoint Logo

Find Current Weather of Any City using OpenWeatherMap API in Python

The OpenWeatherMap is indeed a service that offers weather information to the creators for web services and mobile applications, including current weather information, forecasts, and historical data.

It offers a limited free usage tier as well as an API with JSON, XML, and HTML endpoints. Users can request extended forecasts, graphical maps, and current weather information (showing cloud cover, wind speed, pressure, and precipitation).

Using Python's OpenWeatherMap API, the current weather in any city:

Requests and json modules are necessary.

Method 1: Using the request Module and json:

In this method, we will use request and json module.

Output

Enter city name : Mumbai
 Temperature display(in kelvin unit) = 318.15
 atmospheric pressure display(in hPa unit) = 956
 humidity display(in percentage) = 45
 description = Cloudy

Method 2: Using the request Module and BeautifulSoup

In this method, we will use request and BeautifulSoup module.

Input

Output

Enter the Name of City: Hyderabad
Searching for...
Hyderabad, Telangana
Wednesday, 10:00 pm
Partly Cloudy
28°C
Have a Good Day:)

Explanation:

Here, we'll employ a few of the modules and functions described below in the second strategy,

BeautifulSoup: It is a Python package used for web scraping that allows users to retrieve data from HTML and XML files. From the page's source code, it creates a parse tree that can be used to retrieve information in a more comprehensible and hierarchical way. Use the terminal code below to install the system with a lovely soup library.

Requests

To make HTTP requests in this case, we'll use the requests module in Python. Use the following code in the terminal to install.

We are utilising headers in this case because they contain protocol-specific data that comes before the raw message, which is information collected from the website.

Then, in order to receive the data from Google, we will employ the get() method and supply the Google search and the city name to it. The necessary HTML data from the webpage will then be parsed using BeautifulSoup.

Then, after storing them in a variable, we will employ the select() method to retrieve the specific data, such as time, information, and position.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA