使用Python构建 GUI 应用程序以获取实时空气质量信息。
我们生活在现代化和工业化时代。我们的生活变得越来越方便。但问题是空气污染随着时间的推移而出现。这种污染使我们不健康,空气是我们生命的生命线。
在本文中,我们将编写Python脚本来获取实时空气质量信息并将其与 GUI 应用程序绑定。
需要的模块
- bs4: Beautiful Soup(bs4) 是一个Python库,用于从 HTML 和 XML 文件中提取数据。要安装此类型,请在终端中输入以下命令。
pip install bs4
- 请求:这使您可以非常轻松地发送 HTTP/1.1 请求。要安装此类型,请在终端中输入以下命令。
pip install requests
方法:
- 从给定的 URL 中提取数据形式。选择所需位置后复制 URL。
- 在请求和 Beautiful Soup 模块的帮助下抓取数据。
- 将该数据转换为 HTML 代码。
- 找到所需的详细信息并过滤它们。
执行:
第 1 步:导入所有需要的模块
Python3
# import module
import requests
from bs4 import BeautifulSoup
Python3
# link to extract html data
def getdata(url):
r=requests.get(url)
return r.text
Python3
htmldata = getdata(# write the URL )
soup = BeautifulSoup(htmldata, 'html.parser')
result = (soup.find_all("div", class_="styles__primaryPollutantGraphNumber__2WgP9"))
result
Python3
# 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(" ")
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])
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)
Python3
# import modules
from tkinter import *
import requests
from 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 grey
master = Tk()
master.configure(bg='light grey')
# Variable Classes in tkinter
air_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 Label
Label(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 lebel for class variable
# name using widget Entry
Label(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 widget
b = Button(master, text="Check",
command=airinfo, bg="Blue")
b.grid(row=0, column=2, columnspan=2,
rowspan=2, padx=5, pady=5,)
mainloop()
第 2 步:创建 URL 获取函数
蟒蛇3
# link to extract html data
def getdata(url):
r=requests.get(url)
return r.text
第 3 步:现在将 URL 传递到 getdata函数并将该数据转换为 HTML 代码。这里使用的 URL 是“https://weather.com/en-IN/forecast/air-quality/l/3dbed5c769584b3604a70d40a1a0a9f6ebc99c253d955b548f4978ca101eeca1”
蟒蛇3
htmldata = getdata(# write the URL )
soup = BeautifulSoup(htmldata, 'html.parser')
result = (soup.find_all("div", class_="styles__primaryPollutantGraphNumber__2WgP9"))
result
输出:
[
67
,22
,13
,30
,45
,479
]第 4 步:根据给定的数据过滤您的数据并检查您的空气质量:
蟒蛇3
# 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(" ")
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])
输出:
Air Quality : 85
O3 level : 67
NO2 level : 22
SO2 level : 13
PM2.5 level : 30
PM10 level : 45
co level : 479
第 5 步:现在使用给定的数据分析空气质量:
蟒蛇3
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)
输出:
Satisfactory
Minor breathing discomfort to sensitive people
使用 Tkinter 获取实时空气质量信息的应用程序:此脚本将上述实现实现到 GUI 中。
蟒蛇3
# import modules
from tkinter import *
import requests
from 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 grey
master = Tk()
master.configure(bg='light grey')
# Variable Classes in tkinter
air_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 Label
Label(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 lebel for class variable
# name using widget Entry
Label(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 widget
b = Button(master, text="Check",
command=airinfo, bg="Blue")
b.grid(row=0, column=2, columnspan=2,
rowspan=2, padx=5, pady=5,)
mainloop()
输出: