-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqiscrap.py
45 lines (36 loc) · 1.19 KB
/
aqiscrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
from bs4 import BeautifulSoup
import mysql.connector
obj=mysql.connector.connect(host="localhost",user="root",passwd="saiisking1",database="AQI")
a=obj.cursor()
def get_aqi():
url = 'https://aqicn.org/city/india/hyderabad/central-university/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
aqi_element = soup.find('div', {'class': 'aqivalue'})
if aqi_element:
aqi = aqi_element.text.strip()
level = get_aqi_level(int(aqi))
print(f"Current AQI in Hyderabad: {aqi} ({level})")
query = "insert into aqi (aqi, level) values (%s, %s)"
values = (aqi, level)
a.execute(query, values)
obj.commit()
print("AQI data stored in AQI Database")
else:
print("Unable to retrieve AQI data.")
def get_aqi_level(aqi):
if aqi <= 50:
return "Good"
elif aqi <= 100:
return "Moderate"
elif aqi <= 150:
return "Unhealthy for Sensitive Groups"
elif aqi <= 200:
return "Unhealthy"
elif aqi <= 300:
return "Very Unhealthy"
else:
return "Hazardous"
if __name__ == "__main__" :
get_aqi()