Build an GUI Application to Get Live Air Quality Information Using Python. - GeeksforGeeks

04 Dec, 2021
We are living in a modernization and industrialization era. Our life becomes more and more convenient. But the problem is Air Pollution arise with time. This Pollution makes us unhealthy, Air is a Lifeline for our life.
In this article, we are going to write python scripts to get live air quality information and bind it with GUI Application.
Modules Needed
bs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. To install this type the command below in the terminal.
pip install bs4 
requests: This allows you to send HTTP/1.1 requests very easily. To install this type the command below in the terminal.
pip install requests 
Approach:
Extract data form given URL. Copy the URL, after selecting the desired location.
Scrape the data with the help of requests and Beautiful Soup module.
Convert that data into HTML code.
Find the required details and filter them.
Implementation:Step 1: Import all the modules required
Python3
# import moduleimport requestsfrom bs4 import BeautifulSoup
Step 2: Create a URL get function 
Python3
# link to extract html data     def getdata(url):    r=requests.get(url)    return r.text
Step 3: Now pass the URL into the getdata function and convert that data into HTML code. The URL used here is “https://weather.com/en-IN/forecast/air-quality/l/3dbed5c769584b3604a70d40a1a0a9f6ebc99c253d955b548f4978ca101eeca1”
Python3
htmldata = getdata(# write the URL )soup = BeautifulSoup(htmldata, 'html.parser')result = (soup.find_all("div", class_="styles__primaryPollutantGraphNumber__2WgP9"))result
Output:
[<div class=”styles__primaryPollutantGraphNumber__2WgP9′′ classname=”styles__primaryPollutantGraphNumber__2WgP9′′>67</div>, <div class=”styles__primaryPollutantGraphNumber__2WgP9′′ classname=”styles__primaryPollutantGraphNumber__2WgP9′′>22</div>, <div class=”styles__primaryPollutantGraphNumber__2WgP9′′ classname=”styles__primaryPollutantGraphNumber__2WgP9′′>13</div>, <div class=”styles_N_primaryPollutantGraphNumber__2WgP9′′ classname=”styles__primaryPollutantGraphNumber__2WgP9′′>30</div>, <div class=”styles__primaryPollutantGraphNumber__2WgP9′′ classname=”styles__primaryPollutantGraphNumber__2WgP9′′>45</div>, <div class=”styles__primaryPollutantGraphNumber__2WgP9′′ classname=”styles__primaryPollutantGraphNumber__2WgP9′′>479</div>] 
Step 4: Filter your data and Check your Air Quality according to the given data :
Python3
# Traverse the air qualityfor item in (soup.find_all("div", class_="styles__aqiGraphNumber__2R6Y9")):    res_data = item.get_text() # traverse the contentdata = ''for item in (soup.find_all("div", class_="styles__primaryPollutantGraphNumber__2WgP9")):    data += item.get_text()    data += " "air_data = data.split(" ")print("Air Quality :", res_data)print("O3 level :", air_data[0])print("NO2 level :", air_data[1])print("SO2 level :", air_data[2])print("PM2.5 level :", air_data[3])print("PM10 level :", air_data[4])print("co level :", air_data[5])
Output:
Air Quality : 85
O3 level : 67
NO2 level : 22
SO2 level : 13
PM2.5 level : 30
PM10 level : 45
co level : 479
Step 5: Now Analyze the Air Quality with the given data:
Python3
res = int(res_data) if res <= 50:    remark = "Good"    impact = "Minimal impact"     elif res <= 100 and res > 51:    remark = "Satisfactory"    impact = "Minor breathing discomfort to sensitive people"     elif res <= 200 and res >= 101:    remark = "Moderate"    impact = "Breathing discomfort to the people with lungs, asthma and heart diseases"     elif res <= 400 and res >= 201:    remark = "Very Poor"    impact = "Breathing discomfort to most people on prolonged exposure"     elif res <= 500 and res >= 401:    remark = "Severe"    impact = "Affects healthy people and seriously impacts those with existing diseases"     print(remark)print(impact)
Output:
Satisfactory
Minor breathing discomfort to sensitive people
Application for the live Air Quality information with Tkinter: This Script implements the above Implementation into a GUI.
Python3
# import modulesfrom tkinter import *import requestsfrom bs4 import BeautifulSoup  # link for extract html data def getdata(url):    r = requests.get(url)    return r.text  def airinfo():    htmldata = getdata(        "https://weather.com/en-IN/forecast/air-quality/l/3dbed5c769584b3604a70d40a1a0a9f6ebc99c253d955b548f4978ca101eeca1")    soup = BeautifulSoup(htmldata, 'html.parser')        # Traverse the air quality    for item in (soup.find_all("div",                               class_="styles__aqiGraphNumber__2R6Y9")):        res_data = item.get_text()         # traverse the content    data = ''    for item in (soup.find_all("div",                               class_="styles__primaryPollutantGraphNumber__2WgP9")):        data += item.get_text()        data += " "         air_data = data.split(" ")         ar.set(res_data)    o3.set(air_data[0])    no2.set(air_data[1])    so2.set(air_data[2])    pm.set(air_data[3])    pml.set(air_data[4])    co.set(air_data[5])    res = int(res_data)    if res <= 50:        remark = "Good"        impact = "Minimal impact"    elif res <= 100 and res > 51:        remark = "Satisfactory"        impact = "Minor breathing discomfort to sensitive people"    elif res <= 200 and res >= 101:        remark = "Moderate"        impact = "Breathing discomfort to the people with lungs, asthma and heart diseases"    elif res <= 400 and res >= 201:        remark = "Very Poor"        impact = "Breathing discomfort to most people on prolonged exposure"    elif res <= 500 and res >= 401:        remark = "Severe"        impact = "Affects healthy people and seriously impacts those with existing diseases"    res_remark.set(remark)    res_imp.set(impact)  # object of tkinter# and background set to greymaster = Tk()master.configure(bg='light grey') # Variable Classes in tkinterair_data = StringVar()ar = StringVar()o3 = StringVar()no2 = StringVar()so2 = StringVar()pm = StringVar()pml = StringVar()co = StringVar()res_remark = StringVar()res_imp = StringVar()  # Creating label for each information# name using widget LabelLabel(master, text="Air Quality : ",      bg="light grey").grid(row=0, sticky=W)Label(master, text="O3 (μg/m3) :",      bg="light grey").grid(row=1, sticky=W)Label(master, text="NO2 (μg/m3) :",      bg="light grey").grid(row=2, sticky=W)Label(master, text="SO2 (μg/m3) :",      bg="light grey").grid(row=3, sticky=W)Label(master, text="PM2.5 (μg/m3) :",      bg="light grey").grid(row=4, sticky=W)Label(master, text="PM10 (μg/m3) :",      bg="light grey").grid(row=5, sticky=W)Label(master, text="CO (μg/m3) :",      bg="light grey").grid(row=6, sticky=W) Label(master, text="Remark :",      bg="light grey").grid(row=7, sticky=W)Label(master, text="Possible Health Impacts :",      bg="light grey").grid(row=8, sticky=W)  # Creating label for class variable# name using widget EntryLabel(master, text="", textvariable=ar,      bg="light grey").grid(    row=0, column=1, sticky=W)Label(master, text="", textvariable=o3,      bg="light grey").grid(    row=1, column=1, sticky=W)Label(master, text="", textvariable=no2,      bg="light grey").grid(    row=2, column=1, sticky=W)Label(master, text="", textvariable=so2,      bg="light grey").grid(    row=3, column=1, sticky=W)Label(master, text="", textvariable=pm,      bg="light grey").grid(    row=4, column=1, sticky=W)Label(master, text="", textvariable=pml,      bg="light grey").grid(    row=5, column=1, sticky=W)Label(master, text="", textvariable=co,      bg="light grey").grid(    row=6, column=1, sticky=W)Label(master, text="", textvariable=res_remark,      bg="light grey").grid(row=7, column=1, sticky=W)Label(master, text="", textvariable=res_imp,      bg="light grey").grid(row=8, column=1, sticky=W)  # creating a button using the widgetb = Button(master, text="Check",           command=airinfo, bg="Blue")b.grid(row=0, column=2, columnspan=2,       rowspan=2, padx=5, pady=5,) mainloop()
Output:
nnr223442
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python