Skip to content

Instantly share code, notes, and snippets.

@haoliangyu
Created February 5, 2015 04:38
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 haoliangyu/5c86f798432e168735fc to your computer and use it in GitHub Desktop.
Save haoliangyu/5c86f798432e168735fc to your computer and use it in GitHub Desktop.
Python script to download daily weather data from Korea Meteorological Administration
id,korean,english,latitude,longitude
0,서울,Seoul,37.566535,126.9779692
1,백령도,Baengnyeong,37.9728415,124.718271
2,동두천,Dongducheon,37.9034112,127.0605075
3,문산,Munsan,37.889992,126.6987
4,인천,Incheon,37.4562557,126.7052062
5,수원,Suwon,37.2635727,127.0286009
6,강화,ganghwa,37.7131742,126.4511281
7,양평,Yangpyeong,37.4912195,127.4875607
8,이천,Icheon,37.2719952,127.4348221
9,철원,Cheorwon,38.146609,127.3132256
10,춘천,Chuncheon,37.8813153,127.7299707
11,원주,Wonju,37.3422186,127.9201621
12,영월,Youngwol,37.183637,128.4617535
13,인제,Inje,38.0694675,128.1706991
14,홍천,Hongcheon,37.6969518,127.8886827
15,정선군,Jeongseon,37.3807549,128.6609505
16,속초,Sokcho,38.2070148,128.5918488
17,북강릉,North Gangneung,37.751853,128.8760574
18,강릉,Gangneung,37.751853,128.8760574
19,대관령,Daegwanryeong,37.6735799,128.7061681
20,동해,Donghae,37.5247192,129.1142915
21,울릉도,Ulleung,37.4844171,130.9058002
22,태백,Taebaek,37.1640654,128.9855649
23,청주,Cheongju,36.6424341,127.4890319
24,추풍령,Chupungryeong,35.5445563,128.4922143
25,충주,Chungju,36.9910113,127.9259497
26,제천,Jecheon,37.1325821,128.1909481
27,보은,Boeun,36.4894573,127.7294827
28,서산,Seosan,36.7844993,126.4503169
29,대전,Daejeon,36.3504119,127.3845475
30,천안,Cheonan,36.815129,127.1138939
31,보령,Boryeong,36.3331629,126.6129441
32,부여,Buyeo,36.2754406,126.910178
33,금산,Geumsan,36.1086928,127.4880712
34,군산,Gunsan,35.9676772,126.7366293
35,전주,Jeonju,35.8242238,127.1479532
36,고창,Gochang,35.4358216,126.7020806
37,부안,Buan,35.73156609999999,126.7334651
38,정읍,Jeongeup,35.5698855,126.8558955
39,남원,Namwon,35.416357,127.3904877
40,임실,Imsil,35.6110549,127.2826599
41,장수,Jangsu,35.6472767,127.5211363
42,고창군,Gochang,35.4358216,126.7020806
43,순창군,Sunchang,35.3744136,127.1375968
44,광주,Gwangju,35.1595454,126.8526012
45,목포,Mokpo,34.8118351,126.3921664
46,흑산도,Heuksan,34.6829748,125.4275194
47,완도,Wando,34.3110596,126.7550541
48,여수,Yeosu,34.7603737,127.6622221
49,진도(첨찰산),Jindo,34.4868712,126.2634853
50,순천,Suncheon,34.950637,127.4872135
51,주암,Juam,37.465035,126.679954
52,장흥,Jangheung,34.6816856,126.9069278
53,해남,Haenam,34.5732516,126.5989274
54,강진군,gangjin,34.6420774,126.767261
55,고흥,Goheung,34.6112219,127.284978
56,영광군,Yeonggwang,35.2771719,126.5119874
57,광양시,Gwangyang,34.9406968,127.6958882
58,보성군,Boseong,34.7714563,127.0798944
59,제주,Jeju,33.4996213,126.5311884
60,고산,Gosan,35.9676772,126.7366293
61,서귀포,Seogwipo,33.2541205,126.560076
62,성산,Seongsan,35.89972220000001,128.4611111
63,울진,Uljin,36.9930661,129.4004195
64,상주,Sangju,36.4109466,128.1590828
65,안동,Andong,36.5683543,128.729357
66,대구,Daegu,35.8714354,128.601445
67,포항,Pohang,36.0190178,129.3434808
68,봉화,Bonghwa,36.8930933,128.7323752
69,영주,Yeongju,36.8056858,128.6240551
70,문경,Mungyeong,36.586148,128.1867972
71,영덕,Yeongdeok,36.41507989999999,129.3659681
72,의성,uiseong,36.3526576,128.6970053
73,구미,Gumi,36.119485,128.3445734
74,영천,Yeongcheon,35.97329149999999,128.9385493
75,경주시,Gyeongju,35.8561719,129.2247477
76,청송군,Cheongsong,36.4359045,129.0571077
77,김해시,Gimhae,35.2285451,128.8893517
78,양산시,Yangsan,35.3350072,129.0371689
79,북창원,Bukchang,37.5622798,126.9780753
80,부산,Busan,35.1795543,129.0756416
81,울산,Ulsan,35.5383773,129.3113596
82,창원,Changwon,37.66399759999999,127.9784585
83,통영,Tongyeong,34.8544227,128.433182
84,의령군,Uiryeong,35.3221896,128.261658
85,진주,Jinju,35.1799817,128.1076213
86,거창,Geochang,35.6867229,127.9095155
87,합천,Hapcheon,35.5665758,128.1657995
88,밀양,Miryang,35.5037598,128.7464415
89,산청,Sancheong,35.4155885,127.8734981
90,거제,Geoje,34.8806427,128.6210824
91,남해,Namhae,34.8376721,127.8924234
92,함양군,Hamyang,35.52046139999999,127.7251763
from datetime import date
from bs4 import BeautifulSoup
# Download data from website of KMA
def getkmadata(date):
url = 'http://www.kma.go.kr/weather/observation/currentweather.jsp?auto_man=m&type=t99&tm=%d.%d.%d.20%%3A00&x=23&y=5' % (date.year, date.month, date.day)
# Use BeautifulSoup to interpret htmp page
content = urllib.request.urlopen(url).read()
soup = BeautifulSoup(content)
trs = soup.find_all("tr")[2:]
# Dictionary to store data, which is indexed with station names
weatherdata = {}
for tr in trs:
# name of city (Korean), encoding is recommended to avoid potential problems.
station = tr.contents[1].text.encode('utf-8', 'ignore')
# temperature
temperature = tr.contents[11].text
# humidity
humidity = tr.contents[21].text
# windspeed
windspeed = tr.contents[-4].text
# Other weather condition
# You may add your code here to get other conditions
# wrap the data
weatherdata[station] = (temperature, humidity, windspeed)
return weatherdata
# Downlaod data of 09/12/2012
casedate = date(year = 2012, month = 9, day = 12)
data = getkmadata(casedate)
# print the data
for station, weatherdata in data.items():
# Printing Korean may rise encoding problems.
# print('station name: %s' %s station.decode('utf-8'))
print('temperature: %s' % data[0])
print('humidity: %s' % data[1])
print('windspeed: %s' $ data[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment