-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscraping_ym.py
More file actions
78 lines (62 loc) · 2.68 KB
/
Copy pathscraping_ym.py
File metadata and controls
78 lines (62 loc) · 2.68 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
import requests
import csv
from datetime import datetime, timedelta
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 = ["record_time", "obs_post_id", "obs_post_name", "obs_lat", "obs_lon", "wind_dir", "wind_speed"]
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}")
if __name__ == '__main__':
# API endpoint and required parameters
url = 'http://www.khoa.go.kr/api/oceangrid/tideObsWind/search.do'
authKey = 'zudwtvvTQ3EMLXJjltrCyQ==' # API authentication key
obsCode = 'DT_0005' # Observation Code
# 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:
# Format the date parameter as YYYYMMDD (API accepts date without time)
date_str = start_date.strftime('%Y%m%d')
params = {
'ServiceKey': authKey,
'ObsCode': obsCode,
'Date': date_str,
'ResultType': 'json' # Response format
}
try:
response = requests.get(url, params=params)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx and 5xx)
data = response.json()
if 'result' in data and 'data' in data['result']:
for item in data['result']['data']:
row = [
item.get("record_time", "-99"),
item.get("obs_post_id", "-99"),
item.get("obs_post_name", "-99"),
item.get("obs_lat", "-99"),
item.get("obs_lon", "-99"),
item.get("wind_dir", "-99"),
item.get("wind_speed", "-99")
]
all_data.append(row)
print(f"Processed date: {start_date.strftime('%Y-%m-%d %H:%M')}.")
except Exception as e:
print(f"Error processing date {start_date.strftime('%Y-%m-%d %H:%M')}: {e}")
# Increment the date by 1 hour
start_date += delta
# Save the collected data to a CSV file
output_csv = 'khoa_wind_data.csv'
save_to_csv(all_data, output_csv)