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 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 :
- Extract data from the weather website using requests.
- Parse the HTML using BeautifulSoup.
- Filter the relevant weather information.
- Format the information as a string.
- Show a desktop notification with the result.
Step-by-Step Implementation (Windows)
Step 1: Import all dependence
import requests
from bs4 import BeautifulSoup
from win10toast import ToastNotifier
Step 2: Create an object of ToastNotifier class.
n = ToastNotifier()
Step 3: Define a function for getting data from the given url.
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.
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:

Step 5: Find the required details and filter them
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)
res = "current_temp " + temp[128:-9] + " in patna bihar" + "\n" +temp_rain[131:-14]
Step 6: Now pass the result into notifications object.
n.show_toast("Weather update", res, duration = 10)
Output :

Complete Code:
import requests
from bs4 import BeautifulSoup
from win10toast import ToastNotifier
n = ToastNotifier() # object
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)
res = "current_temp " + temp[128:-9] + " in patna bihar" + "\n" + temp_rain[131:-14]
n.show_toast("live Weather update", res, duration=10)
Output

Cross-Platform Support for macOS/Linux
Since win10toast only works on Windows, you can use the plyer module for notifications on macOS, Linux and Windows.
Step 1: Install Required Modules
pip install requests
pip install bs4
pip install plyer
pip install requests
pip install bs4
pip install plyer
Step 2: Import All Dependencies
import requests
from bs4 import BeautifulSoup
from plyer import notification
import requests
from bs4 import BeautifulSoup
from plyer import notification
Step 3: Create a Function to Get Data from the URL
def getdata(url):
r = requests.get(url)
return r.text
def getdata(url):
r = requests.get(url)
return r.text
Step 4: Fetch and Parse the HTML Data
htmldata = getdata("https://weather.com/en-IN/weather/today/l/25.59,85.14?par=google&temp=c/")
soup = BeautifulSoup(htmldata, 'html.parser')
htmldata = getdata("https://weather.com/en-IN/weather/today/l/25.59,85.14?par=google&temp=c/")
soup = BeautifulSoup(htmldata, 'html.parser')
Step 5: Filter the Required Weather Details
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)
res = "Current Temperature: " + temp[128:-9] + " in Patna, Bihar" + "\n" + "Rain Chances: " + temp_rain[131:-14]
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)
res = "Current Temperature: " + temp[128:-9] + " in Patna, Bihar" + "\n" + "Rain Chances: " + temp_rain[131:-14]
Step 6: Display the Desktop Notification
notification.notify(
title="live weather update",
message=res,
timeout=10
)
notification.notify(
title="live weather update",
message=res,
timeout=10
)
Complete code:
import requests
from bs4 import BeautifulSoup
from plyer import notification
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)
res = "Current Temperature: " + temp[128:-9] + " in Patna, Bihar" + "\n" + "Rain Chances: " + temp_rain[131:-14]
notification.notify(
title="live weather update",
message=res,
timeout=10
)
import requests
from bs4 import BeautifulSoup
from plyer import notification
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)
res = "Current Temperature: " + temp[128:-9] + " in Patna, Bihar" + "\n" + "Rain Chances: " + temp_rain[131:-14]
notification.notify(
title="live weather update",
message=res,
timeout=10
)
Output
