The Wayback Machine - https://web.archive.org/web/20240625000049/https://www.geeksforgeeks.org/get-live-weather-desktop-notifications-using-python/
Open In App

Get Live Weather Desktop Notifications Using Python

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 for pulling data out of HTML and XML files. To install this module type the below command in the terminal.
pip install bs4
  • win10toast: This library helps in creating desktop notifications. To install this module type the below command in the terminal.
pip install win10toast
  • requests: This library allows you to send HTTP/1.1 requests extremely easily. To install this module type the below command in the terminal.
pip install requests

Approach :

  1. Extract data form given URL.
  2. Scrape the data with the help of requests and Beautiful Soup.
  3. Convert that data into html code.
  4. Find the required details and filter them.
  5. Save the result in the String.
  6. Pass the result in Notification object.

Let’s execute the script step-by-step :

Step 1: Import all dependence

Python3




import requests
from bs4 import BeautifulSoup
from win10toast import ToastNotifier


Step 2: Create an object of ToastNotifier class.

Python3




n = ToastNotifier()


Step 3: Define a function for getting data from the given url.

Python3




def getdata(url):
    
    r = requests.get(url)
      
    return r.text


Step 4: Now pass the URL into the getdata function and Convert that data into HTML code.

Python3




htmldata = getdata("https://weather.com/en-IN/weather/today/l/25.59,85.14?par=google&temp;=c/")
  
soup = BeautifulSoup(htmldata, 'html.parser')
  
print(soup.prettify())


After executing this script you will get raw data like these:

Raw HTML information 

Step 5: Find the required details and filter them 

Python3




current_temp = soup.find_all("span", 
                             class_=" _-_-components-src-organism-CurrentConditions-CurrentConditions--tempValue--MHmYY")
chances_rain = soup.find_all("div", 
                             class_= "_-_-components-src-organism-CurrentConditions-CurrentConditions--precipValue--2aJSf")
  
temp = (str(current_temp))   
temp_rain = str(chances_rain)
  
result = "current_temp " + temp[128:-9] + "  in patna bihar" + "\n" +temp_rain[131:-14]


Step 6: Now pass the result into notifications object.

Python3




n.show_toast("Weather update", result, duration = 10)


Output :

notification 

Complete Code:

Python3




# import required libraries
import requests
from bs4 import BeautifulSoup
from win10toast import ToastNotifier
  
# create an object to ToastNotifier class
n = ToastNotifier()
  
# define a function
def getdata(url):
    r = requests.get(url)
    return r.text
    
htmldata = getdata("https://weather.com/en-IN/weather/today/l/25.59,85.14?par=google&temp;=c/")
  
soup = BeautifulSoup(htmldata, 'html.parser')
  
current_temp = soup.find_all("span", class_= "_-_-components-src-organism-CurrentConditions-CurrentConditions--tempValue--MHmYY")
  
chances_rain = soup.find_all("div", class_= "_-_-components-src-organism-CurrentConditions-CurrentConditions--precipValue--2aJSf")
  
temp = (str(current_temp))
  
temp_rain = str(chances_rain)
   
result = "current_temp " + temp[128:-9] + "  in patna bihar" + "\n" + temp_rain[131:-14]
n.show_toast("live Weather update", 
             result, duration = 10)


Output:

Live Notification



Previous Article
Next Article

Similar Reads

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
Windows 10 Toast Notifications with Python
Python is a general-purpose language, can be used to develop both desktop and web applications. By using a package available in Python named win10toast , we can create desktop notifications. It is an easy way to get notified when some event occurs. The package is available in Pypi and it is installed using pip. pip install win10toast About show_toa
2 min read
Build a GUI Application to Get Live Stock Price using Python
The stock price is the highest amount someone is willing to pay for the stock. In this article, we are going to write code for getting live share prices for each company and bind it with GUI Application. Module Needed Yahoo_fin: This module is used to scrape historical stock price data, as well as to provide current information on market caps, divi
3 min read
Build an GUI Application to Get Live Air Quality Information Using Python.
We are living in a modernization and industrialization era. Our life becomes more and more convenient. But the problem is Air Pollution arise with time. This Pollution makes us unhealthy, Air is a Lifeline for our life. In this article, we are going to write python scripts to get live air quality information and bind it with GUI Application. Module
5 min read
Application to get live USD/INR rate Using Python
In this article, we are going to write a python scripts to get live information of USD/INR rate and bind with it GUI application. Modules Required:bs4: Beautiful Soup is a Python library for pulling data out of HTML and XML files. Installation: pip install bs4requests: This module allows you to send HTTP/1.1 requests very easily. Installation: pip
3 min read
Get live train status using Python
Suppose you want to travel to places using Indian Railways and have booked a train. But you are not sure that the train is on time or not and doing this manually can be very hectic. So in this article, we are going to write a Python script to get live train status using a train name or train code. Modules neededbs4: Beautiful Soup(bs4) is a Python
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
three90RightbarBannerImg