Get Live Weather Desktop Notifications Using Python (original) (raw)

Last Updated : 29 Dec, 2020

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

pip install bs4

pip install win10toast

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

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

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 requests

from bs4 import BeautifulSoup

from win10toast import ToastNotifier

n = ToastNotifier()

def getdata(url):

`` r = requests.get(url)

`` return r.text

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

Similar Reads

Projects for Beginners
























Projects for Intermediate


















Web Scraping












Automating boring Stuff Using Python

















Tkinter Projects



































Turtle Projects












OpenCV Projects

































Python Django Projects














Python Text to Speech and Vice-Versa