Smart Weather Information App Using Python

Smart Weather Information App Using Python


Published at - Jan 12, 2022

PYTHON SCRIPTS

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

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

ToastNotifier

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

pip install win10toast

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

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'}

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)

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 "

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)

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 me to 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.


Smart Weather Information App Using Python was originally published in Python in Plain English on Medium, where people are continuing the conversation by highlighting and responding to this story.





Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile