Repository navigation
Expand file tree
/
Copy pathscraping_sj.py
More file actions
108 lines (91 loc) · 4.03 KB
/
Copy pathscraping_sj.py
File metadata and controls
108 lines (91 loc) · 4.03 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import scraping
import requests
from bs4 import BeautifulSoup
import csv
from datetime import datetime, timedelta
# # params = dict()
# def getJson(url, params={}):
# res = getRequest(url, params)
# return res.json()
def save_to_csv(data_list, output_file):
"""
Saves a list of data dictionaries to a CSV file.
Args:
data_list (list): List of data rows.
output_file (str): Path to the output CSV file.
"""
if not data_list:
print("No data to save.")
return
header = [
"YYMMDDHHMI", "STN", "LON", "LAT", "WD", "WS", "WD_INS", "WS_INS", "INS_TM",
"TA", "TA_MIN", "TA_MIN_TM", "TA_MAX", "TA_MAX_TM", "HM", "PA", "RN", "TW",
"WH_MAX", "WH_SIG", "WH_AVE", "WO", "WP", "SI", "SS", "VT"
]
with open(output_file, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(header)
writer.writerows(data_list)
print(f"Data successfully saved to {output_file}")
# https://apihub.kma.go.kr/api/typ01/url/kma_kship.php?tm=202211301200&stn=22003&help=1&authKey=JfSBF3K5SuG0gRdyudrhsw
if __name__ == '__main__':
# API endpoint and required parameters
url = 'https://apihub.kma.go.kr/api/typ01/url/kma_kship.php'
authKey = 'JfSBF3K5SuG0gRdyudrhsw' # API authentication key
stn = '0' # Station ID (e.g., 22003 for 기상1호, 1 for 기상2000호)
# Date range for data retrieval
start_date = datetime.strptime('2023-01-01 00:00', '%Y-%m-%d %H:%M')
end_date = datetime.strptime('2023-01-03 00:00', '%Y-%m-%d %H:%M')
delta = timedelta(hours=1) # Time step for hourly iteration
all_data = []
while start_date <= end_date:
tm = start_date.strftime('%Y%m%d%H%M') # Format: YYYYMMDDHHMM
params = {
'tm': tm, # Timestamp in the format YYYYMMDDHHMM (KST)
'stn': stn, # Station ID for specific or all stations
'help': 1, # Add field descriptions (1 for enabled, 0 or absent for disabled)
'authKey': authKey # API authentication key
}
data = scraping.getJson(url, params)
if data and 'response' in data:
response_data = data['response']
if isinstance(response_data, list):
for item in response_data:
lon = float(item.get("LON", 0))
lat = float(item.get("LAT", 0))
# Skip rows where LON and LAT are 0.000000
if lon == 0.0 and lat == 0.0:
continue
row = [
item.get("YYMMDDHHMI", "-99"),
item.get("STN", "-99"),
item.get("LON", "-99"),
item.get("LAT", "-99"),
item.get("WD", "-99"),
item.get("WS", "-99"),
item.get("WD_INS", "-99"),
item.get("WS_INS", "-99"),
item.get("INS_TM", "-99"),
item.get("TA", "-99"),
item.get("TA_MIN", "-99"),
item.get("TA_MIN_TM", "-99"),
item.get("TA_MAX", "-99"),
item.get("TA_MAX_TM", "-99"),
item.get("HM", "-99"),
item.get("PA", "-99"),
item.get("RN", "-99"),
item.get("TW", "-99"),
item.get("WH_MAX", "-99"),
item.get("WH_SIG", "-99"),
item.get("WH_AVE", "-99"),
item.get("WO", "-99"),
item.get("WP", "-99"),
item.get("SI", "-99"),
item.get("SS", "-99"),
item.get("VT", "-99")
]
all_data.append(row)
print(f"Processed date: {start_date.strftime('%Y-%m-%d %H:%M')}.")
start_date += delta
output_csv = 'kma_kship_data.csv'
save_to_csv(all_data, output_csv)