How to get current weather information using Python?

 In today's busy lifestyle, we need to choose a smart way of getting weather information. You can do it using a couple of lines. This is a good idea, right? 😊

We need the following things to implement the above small project.

  1. What is a Beautifulsoup library?  Beautiful Soup is a Python library for pulling data out of HTML and XML files. It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree.

    To install Beautifulsoup: pip install bs4

  2.  What is the requests library?  The library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.

    To install requests: pip install requests

  3.  What is the time module? This module provides various time-related functions. The time module comes with Python's standard utility module, so there is no need to install it externally. You can simply import it using an import statement. 

  4. What is win10toast? win10toast is a package. By using win10toast you can create a desktop notification. It is an easy way to get notified when some event occurs. To create a notification you have to import win10toast. Then create an object of ToastNotifier class. You can easily create a notification using the show_toast method. This method contains the header or title of notification, actual message, duration, of that notification, and icon for that notification.  

         To install win10toast: pip install win10toast

A simple example of win10toast:

# import win10toast
from win10toast import ToastNotifier

# create an object to ToastNotifier class
n = ToastNotifier()

n.show_toast("Win10Toast", "You got notification", duration = 10)



How to get your city weather information?


# Import libraries and modules

from bs4 import BeautifulSoup
import requests
import time
from win10toast import ToastNotifier

# define header

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

def get_weather_info(city):
    city=city.replace(" ","+")
# get url
    res = requests.get(f'https://www.google.com/search?q={city}&oq={city}&aqs=chrome.0.35i39l2j0l4j46j69i60.6128j1j7&sourceid=chrome&ie=UTF-8',headers=headers)
# cretae a BeautifulSoup object
    soup = BeautifulSoup(res.text,'html.parser')   
# data scraping
    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()
    info = f"{location} \n {current_time} \n {info} \n {weather} °C "
 
# create an object to ToastNotifier class       
    toaster = ToastNotifier()
# create the notification
    toaster.show_toast("Weather Information",
    f"{info}",
    duration=10,
    threaded=True)
    while toaster.notification_active(): time.sleep(0.005)   

# Take input from user.
city = input('Enter your city name: ')
city=city+" weather"
get_weather_info(city)





Thank you 😊 for reading. Please read other blogs. And also share with your friends and 
family.
            

Comments

Popular posts from this blog

How to convert PDF file into audio file?

How to perform operations on emails and folders using imap_tools?

Pillow Libary in Python.