Created
March 17, 2015 07:47
-
-
Save stripe-q/8d4ef2bf4bd1566ff602 to your computer and use it in GitHub Desktop.
대기오염 정보
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"charts": [{ | |
"I10003_GRADE": "1", // 오존 등급 | |
"I10002_GRADE": "1", // 일산화 탄소 등급 | |
"KHAI_GRADE": "4", // 통합 대기 환경 등급 | |
"PERIOD": "1", // 시간? | |
"KHAI_VALUE": "303", // 통합 대기 환경 지수 | |
"I10006_GRADE": "3", // 이산화 질소 | |
"I10001_GRADE": "1", // 아황산 가스 | |
"VALUE_10007": "223", // 미세먼지(PM10) 농도값 | |
"VALUE_24AVG_10008": null, // PM2.5의 24시간 평균 | |
"VALUE_10008": null, // PM2.5의 농도값 | |
"VALUE_10006": "0.080", // 이산화질소 ppm | |
"VALUE_24AVG_10007": "154", // PM10 미세먼지 평균 | |
"DATADIV": "1", // 단위? | |
"DATA_TIME": "03-17:12", // 측정일시 | |
"I10008_GRADE": null, // PM2.5 등급 | |
"VALUE_10003": "0.014", // 오존 농도값 | |
"I10007_GRADE": "4", // 미세먼지 등급 | |
"VALUE_10001": "0.017", // 아황산 가스 농도 | |
"DATA_TIME_CHART": "2015/03/17 11:00", | |
"VALUE_10002": "1.1", // 일산화탄소 | |
"KHAI_ITEM_CODE": "10007" // 주요오염물질 코드 | |
}, | |
... | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
""" | |
지역별(경기) 매 시 미세먼지양 정보 | |
""" | |
from datetime import datetime | |
from urllib.request import urlopen | |
from bs4 import BeautifulSoup | |
def get_raw_data(): | |
now = datetime.now() | |
today_str = "{:04d}-{:02d}-{:02d}".format(now.year, now.month, now.day) | |
g_URL = "http://www.airkorea.or.kr/sido_compare_p01?itemCode=10007&ymd={}%2023&areaCode=031".format(today_str) | |
html_str = urlopen(g_URL).read().decode('utf-8') | |
return html_str | |
def get_data_where(location_): | |
html = get_raw_data() | |
soup = BeautifulSoup(html) | |
trs = [tr for tr in soup.find_all('tr') if location_ in tr.text] | |
if trs: | |
data = [x for x in trs[0].text.strip().split('\n')] | |
data[1:] = [int(x) for x in data[1:] if x != '-'] | |
return data | |
return None | |
def print_data_where(location_): | |
data = get_data_where(location_) | |
for i, d in enumerate(data[1:]): | |
print("{}시: {}".format(i+1, d)) | |
print_data_where("통진") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.request import Request, urlopen | |
import json | |
def get_real_time_info(): | |
URL = "http://www.airkorea.or.kr/web/pollution/getRealChart?dateDiv=1&period=1&stationCode=823652" | |
res = urlopen(URL) | |
raw_data = res.read().decode('utf-8') | |
data = json.loads(raw_data) | |
serial_data = sorted(data['charts'], key=lambda x:x['DATA_TIME_CHART'], reverse=True); | |
p = serial_data[0] | |
""" | |
미세먼지 코드 : 100007 | |
""" | |
print(""" | |
최종측정: {DATA_TIME_CHART} | |
대기오염등급: {KHAI_GRADE} | |
미세먼지등급: {I10007_GRADE} | |
미세먼지농도: {VALUE_10007}""".format(**p)) | |
return p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment