Open In App

Find current weather of any city using OpenWeatherMap API in Python

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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).

You can follow the Weather Forecast Project to create your own weather application using Weather API using HTML, CSS and JavaScript.

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.

Current weather of any city using OpenWeatherMap API in Python

Modules Needed : 

  • requests
  • json

Method 1: Using the json and request module

Python3




# import required modules
import requests, json
 
# Enter your API key here
api_key = "Your_API_Key"
 
# base_url variable to store url
 
# Give city name
city_name = input("Enter city name : ")
 
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
 
# get method of requests module
# return response object
response = requests.get(complete_url)
 
# json method of response object
# convert json format data into
# python format data
x = response.json()
 
# Now x contains list of nested dictionaries
# Check the value of "cod" key is equal to
# "404", means city is found otherwise,
# city is not found
if x["cod"] != "404":
 
    # store the value of "main"
    # key in variable y
    y = x["main"]
 
    # store the value corresponding
    # to the "temp" key of y
    current_temperature = y["temp"]
 
    # store the value corresponding
    # to the "pressure" key of y
    current_pressure = y["pressure"]
 
    # store the value corresponding
    # to the "humidity" key of y
    current_humidity = y["humidity"]
 
    # store the value of "weather"
    # key in variable z
    z = x["weather"]
 
    # store the value corresponding
    # to the "description" key at
    # the 0th index of z
    weather_description = z[0]["description"]
 
    # print following values
    print(" Temperature (in kelvin unit) = " +
                    str(current_temperature) +
          "\n atmospheric pressure (in hPa unit) = " +
                    str(current_pressure) +
          "\n humidity (in percentage) = " +
                    str(current_humidity) +
          "\n description = " +
                    str(weather_description))
 
else:
    print(" City Not Found ")


Output : 

 Enter city name : Delhi
Temperature (in kelvin unit) = 312.15
atmospheric pressure (in hPa unit) = 996
humidity (in percentage) = 40
description = haze

 Method  2: Using BeautifulSoup and request module

Python3




from bs4 import BeautifulSoup
import requests
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
 
 
def weather(city):
    city = city.replace(" ", "+")
    res = requests.get(
        f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8', headers=headers)
    print("Searching...\n")
    soup = BeautifulSoup(res.text, 'html.parser')
    location = soup.select('#wob_loc')[0].getText().strip()
    time = soup.select('#wob_dts')[0].getText().strip()
    info = soup.select('#wob_dc')[0].getText().strip()
    weather = soup.select('#wob_tm')[0].getText().strip()
    print(location)
    print(time)
    print(info)
    print(weather+"°C")
 
 
city = input("Enter the Name of City ->  ")
city = city+" weather"
weather(city)
print("Have a Nice Day:)")
 
# This code is contributed by adityatri


Sample Input:

Mahoba

Sample Output:

Enter the Name of City ->  Mahoba
Searching...

Mahoba, Uttar Pradesh
Monday, 12:00 am
Cloudy
27°C
Have a Nice Day:)

Explanation:

Here in the second approach, we will use some of the following modules and functions as listed below,

  • 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
  • Requests: Here we will use Python’s requests module to make HTTP requests. For installing use the code below in the terminal.
  • 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.
  • 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. 
  • 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.


Similar Reads

Create a GUI for Weather Forecast using openweathermap API in Python
Prerequisites: Find current weather of any city using openweathermap API The idea of this article is to provide a simple GUI application to users to get the current temperature of any city they wish to see. The system also provides a simple user interface for simplification of application. It also provides an amazing UX for its users. The features
4 min read
Weather Union API(Zamato) with Python
Weather Union provides powerful APIs for integrating live weather data into your applications. With a wide range of endpoints, you can access current weather data at specific locations. This guide will walk you through the various APIs offered by Weather Union and provide examples of how to use them in Python. What is Weather Union & Its Featur
3 min read
Python | Real time weather detection using Tkinter
Prerequisites : Introduction to tkinter | Find current weather of any cityPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest
5 min read
Create a Weather app using Flask | Python
Prerequisite : Flask installation Flask is a lightweight framework written in Python. It is lightweight because it does not require particular tools or libraries and allow rapid web development. today we will create a weather app using flask as a web framework. this weather web app will provide current weather updates of cities searched. Basic setu
2 min read
Weather app using Django | Python
In this tutorial, we will learn how to create a Weather app that uses Django as backend. Django provides a Python Web framework based web framework that allows rapid development and clean, pragmatic design. Basic Setup - Change directory to weather – cd weather Start the server - python manage.py runserver To check whether the server is running or
2 min read
Scraping Weather prediction Data using Python and BS4
This article revolves around scraping weather prediction d data using python and bs4 library. Let's checkout components used in the script - BeautifulSoup- It is a powerful Python library for pulling out data from HTML/XML files. It creates a parse tree for parsed pages that can be used to extract data from HTML/XML files. Requests - It is a Python
3 min read
Get Live Weather Desktop Notifications Using Python
We know weather updates are how much important in our day-to-day life. So, We are introducing the logic and script with some easiest way to understand for everyone. Let’s see a simple Python script to show the live update for Weather information. Modules Needed In this script, we are using some libraries bs4: Beautiful Soup(bs4) is a Python library
3 min read
Weather App in Python using Tkinter module
In this article, we are going to discuss how to create a weather app using tkinter. The GUI app will tell us the current weather of a particular city along with temperature details along with other details. Modules required:Tkinter: It is a built-in python library for making GUI using tkinter toolkit.Requests: It is a library which helps in fetchin
4 min read
Scraping weather data using Python to get umbrella reminder on email
In this article, we are going to see how to scrape weather data using Python and get reminders on email. If the weather condition is rainy or cloudy this program will send you an "umbrella reminder" to your email reminding you to pack an umbrella before leaving the house. We will scrape the weather information from Google using bs4 and requests lib
5 min read
Building A Weather CLI Using Python
Trying to know the weather is something that we all do, every day. But have you ever dreamed of making a tool on your own that can do the same? If yes, then this article is for you. In this article, we will look at a step-by-step guide on how to build a Weather CLI using OpenWeather API. What is OpenWeather API?OpenWeather API is a weather API prov
4 min read
Practice Tags :