Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to use pynput to make a Keylogger?
Next article icon

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 from the weather website using requests.
  2. Parse the HTML using BeautifulSoup.
  3. Filter the relevant weather information.
  4. Format the information as a string.
  5. Show a desktop notification with the result.

Step-by-Step Implementation (Windows)

Step 1:Import all dependence

Python
importrequestsfrombs4importBeautifulSoupfromwin10toastimportToastNotifier

Step 2:Create an object of ToastNotifier class.

Python
n=ToastNotifier()

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

Python
defgetdata(url):r=requests.get(url)returnr.text

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

Python
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 

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

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

Output:

notification 

Complete Code:

Python
importrequestsfrombs4importBeautifulSoupfromwin10toastimportToastNotifiern=ToastNotifier()# objectdefgetdata(url):r=requests.get(url)returnr.texthtmldata=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

Live Notification

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

Python
pipinstallrequestspipinstallbs4pipinstallplyer

Step 2: Import All Dependencies

Python
importrequestsfrombs4importBeautifulSoupfromplyerimportnotification

Step 3:Create a Function to Get Data from the URL

Python
defgetdata(url):r=requests.get(url)returnr.text

Step 4: Fetch and Parse the HTML Data

Python
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

Python
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

Python
notification.notify(title="live weather update",message=res,timeout=10)

Complete code:

Python
importrequestsfrombs4importBeautifulSoupfromplyerimportnotificationdefgetdata(url):r=requests.get(url)returnr.texthtmldata=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


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp