Hello, I'm studying open API data, and even if I google it, it doesn't print as I want, so I'm asking you a question. The source code is the actual water temperature code provided by the Korea Maritime Administration. I want to print it out every 4 hours, but there is a variable called record_time in the output value, not the request variable. This variable is included in the year 20200925 08:01:00, so I tried datetime.delta Incompatible with Pandas. I leave a message like this after many trials and errors. Here's the code.
import requests import pandas as pd import datetime
url = "http://www.khoa.go.kr/oceangrid/grid/api/tideObsTemp/search.do"
headers = { 'cache-control': "no-cache", 'postman-token': "96839df0-3d9a-c381-b166-2250742a86d8" }
dt_index = pd.date_range(start='20200922', end='20200923') dt_list = dt_index.strftime("%Y%m%d").tolist()
for i in dt_list: date=i querystring = {"ServiceKey":"OHVMK3dF0Ah6K/oIG71f0g==","ObsCode":"DT_0001","Date":date,"ResultType":"json"} response = requests.request("GET", url, headers=headers, params=querystring) print(response.text)
From:
from pprint import pprint
import requests
import pandas as pd
import datetime
url = "http://www.khoa.go.kr/oceangrid/grid/api/tideObsTemp/search.do"
headers = {
'cache-control': "no-cache",
'postman-token': "96839df0-3d9a-c381-b166-2250742a86d8"
}
dt_index = pd.date_range(start='20200922', end='20200923')
dt_list = dt_index.strftime("%Y%m%d").tolist()
dat = []
for i in dt_list:
date=i
querystring = {"ServiceKey":"=================","ObsCode":"DT_0001","Date":date,"ResultType":"json"}
response = requests.request("GET", url, headers=headers, params=querystring)
#print(response.text)
#print(response.json())
dat.extend(response.json()["result"]["data"])
df = pd.DataFrame(dat)
df.info()
df["record_time"] = pd.to_datetime(df["record_time"])
df["water_temp"] = df["water_temp"].astype(float)
df.info()
print(df)
"""
record_time water_temp
0 2020-09-22 00:00:00 23.5
1 2020-09-22 00:01:00 23.5
2 2020-09-22 00:02:00 23.5
3 2020-09-22 00:03:00 23.5
4 2020-09-22 00:04:00 23.5
... ... ...
2877 2020-09-23 23:56:00 23.1
2878 2020-09-23 23:57:00 23.1
2879 2020-09-23 23:58:00 23.1
2880 2020-09-23 23:59:00 23.1
2881 2020-09-24 00:00:00 23.1
"""
df4hours = df.set_index("record_time").resample("4H").mean()
df4hours.info()
print(df4hours)
"""
water_temp
record_time
2020-09-22 00:00:00 23.355833
2020-09-22 04:00:00 23.174167
2020-09-22 08:00:00 23.153333
2020-09-22 12:00:00 23.393333
2020-09-22 16:00:00 23.367083
2020-09-22 20:00:00 23.331250
2020-09-23 00:00:00 23.302905
2020-09-23 04:00:00 23.101250
2020-09-23 08:00:00 23.014583
2020-09-23 12:00:00 23.150000
2020-09-23 16:00:00 23.129167
2020-09-23 20:00:00 23.040833
2020-09-24 00:00:00 23.100000
"""
If you use the json() method in the response object, the return value in the json format is returned as python dict.
© 2024 OneMinuteCode. All rights reserved.