Skip to content

Instantly share code, notes, and snippets.

@stripe-q
Created March 17, 2015 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stripe-q/fc197c1c87bdfcb80411 to your computer and use it in GitHub Desktop.
Save stripe-q/fc197c1c87bdfcb80411 to your computer and use it in GitHub Desktop.
지역별 미세먼지 지수
#!c:/python34/python.exe
#coding:utf-8
from urllib.request import urlopen
from bs4 import BeautifulSoup
from datetime import datetime
URL = "http://www.airkorea.or.kr/sido_compare_p01?itemCode=10007&ymd={}%2023&areaCode=031"
def make_datestamp():
now = datetime.now()
result = "{:04d}-{:02d}-{:02d}".format(now.year, now.month, now.day)
return result
def get_raw_data():
_url = URL.format(make_datestamp())
_html = urlopen(_url).read().decode('utf-8')
soup = BeautifulSoup(_html)
return soup.find_all('tr')
def get_hour_data(region):
data = get_raw_data()
p = [x.text for x in data if region in x.text]
if p:
row = p[0].strip().split('\n')
row[1:] = [int(x) for x in row[1:] if x != '-']
return (row[0], row[1:])
return (None, [])
tongin = get_hour_data('통진')
if __name__ == "__main__":
tongjin = get_hour_data('통진')
if tongjin[0]:
print(tongjin[0])
for i, d in enumerate(tongjin[1]):
print("{:02d}시 {:03d}".format(i+1, d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment