DEV Community

Harendra Kumar Kanojiya
Harendra Kumar Kanojiya

Posted on • Originally published at python.plainenglish.io on

Smart Weather Information App Using Python

PYTHON SCRIPTS

Python app to show weather notification


Designed with canva.com

Nobody likes to get stranded in the rain or snow. Everyone likes to be kept up to speed on the weather prediction. When you open your computer, this automation script will transmit weather data as a desktop notice.

In this app, we will scrap weather data from google search with the help of the BeautifulSoup Python package. To create a weather app we are going to use the following Python packages πŸ‘‡

Packages

Requests

Requests, a package that makes sending HTTP requests easier and more human-friendly by establishing a connection between the client and the target server with a single line of code.

pip install requests
Enter fullscreen mode Exit fullscreen mode

Beautiful Soup

Beautiful Soup is a Python package used for web scraping to extract data from HTML and XML files. It generates a parse tree from page source code, which may be used to extract data in a more hierarchical and legible manner.

pip install beautifulsoup4
Enter fullscreen mode Exit fullscreen mode

ToastNotifier

A simple Python module for showing Windows 10 Toast Notifications, suitable for Windows GUI development.

pip install win10toast
Enter fullscreen mode Exit fullscreen mode

Steps

Import Packages

We have installed all required packed in this app by runningpip install package_name . Now we have to import those packages in our app at the very start of the app file.

from bs4 import BeautifulSoup
import requests
import time
from win10toast import ToastNotifier
Enter fullscreen mode Exit fullscreen mode

Define headers

The python requests required to define custom headers otherwise, it will give 403 forbidden an error when we try to hit an URL.

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'}
Enter fullscreen mode Exit fullscreen mode

Fetch Url

Now in this step, we have to fetch the URL with the requests module.

res = requests.get(f'[https://www.google.com/search?q=london&oq=london&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8',headers=headers](https://www.google.com/search?q=%7Bcity%7D&oq=%7Bcity%7D&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8',headers=headers))
Enter fullscreen mode Exit fullscreen mode

Parse Weather information

From the previous step, we got raw data from the page. Now we have to parse this data into useful data.

Let’s see the screenshot, we have to scrap the weather section from this page.


Screenshot

soup = BeautifulSoup(res.text,'html.parser')   
    location = soup.select('#wob_loc')[0].getText().strip()  
    current_time = soup.select('#wob_dts')[0].getText().strip()       
    info = soup.select('#wob_dc')[0].getText().strip() 
    weather = soup.select('#wob_tm')[0].getText().strip()
    information = f"{location} \n {current_time} \n {info} \n {weather} Β°C "
Enter fullscreen mode Exit fullscreen mode

Show toast

This is the final step, in this step we have to show a notification with the help of win10tost package previously installed

toaster = ToastNotifier()
toaster.show_toast("Weather Information",
    f"{information}",
    duration=10,
    threaded=True)
    while toaster.notification_active(): time.sleep(0.005)
Enter fullscreen mode Exit fullscreen mode

Full Script

https://medium.com/media/bbb24e6da71090281b94174384ea44a6/href

Summary

In this app, we will scrap weather data from google search with the help of the BeautifulSoup Python package. When you open your computer, this automation script will transmit weather data as a desktop notice. To create a weather app we are going to use the following Python packages.

I hope you enjoyed reading this lesson; you can follow meto read more tutorials from me in the future. Thank you for your time.

_By becoming a Medium member, you can support me and your other favorite authors. Thanks! _ πŸ‘‡

Join Medium with my referral link - Harendra Verma

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.


Discussion (0)