Open API data error

Asked 2 years ago, Updated 2 years ago, 90 views

Hello, while studying the weather data of the Korea Meteorological Administration, this time I'm going to retrieve the data from the National Water Resources Management System (WAMIS) and save it as an Excel file. However, there is an unknown error, so even if you google it, it cannot be solved by yourself, so I ask for your help.

Here's the code.

from pprint import pprint

import requests import pandas as pd import datetime

url = "http://www.wamis.go.kr:8080/wamis/openapi/wkw/we_hrdata%0A"

headers = { 'cache-control': "no-cache", 'postman-token': "4aafe598-6742-a796-5ab9-4e0026a0815d" }

dt_index = pd.date_range(start='20201005', end='20201006') dt_list = dt_index.strftime("%Y%m%d").tolist()

dat = [] for i in dt_list : date = i querystring = {"ServiceKey":"d6ac320da07b282bde61a946be84fe99dfa71ffd19","ResultType":"json","obscd":"10011100","startdt":"20201005","enddt":"20201006"} 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["ymdh"] = pd.to_datetime(df["ymdh"]) df["ta"] = df["ta"].astype(float) df["hm"] = df["hm"].astype(float) df["td"] = df["td"].astype(float) df["ps"] = df["ps"].astype(float) df["ws"] = df["ws"].astype(float) df["wd"] = df["wd"].astype(varchar) df["sihr1"] = df["sihr1"].astype(float) df["catot"] = df["catot"].astype(float) df["sdtot"] = df["sdtot"].astype(float) df["sshr1"] = df["sshr1"].astype(float)

df4hours = df.set_index("ymdh").resample("4H").mean()

df4hours.to_excel('weathertime.xlsx')

See

, visit : http://www.wamis.go.kr:8080/wamisweb/we/w12.do

.

This code is intended to contain weather information codes that are grouped into dates of the year and printed in 4 hours using Pandas in Excel. But here, dat.extend(response.json()["result"]["data"]) This phrase says data is a key error, but I don't know where it went wrong.

api weather

2022-09-20 19:54

1 Answers

import requests
import pandas as pd
import datetime
import json
import sys

def convert_strtdat(data):
    try:
        if data[-2:] == '24':
            d = datetime.datetime.strptime(data[:-2], '%Y%m%d')
            d = d + datetime.timedelta(days=1)
        else:
            d = datetime.datetime.strptime(data, '%Y%m%d%H')
        return d
    except Exception as e:
        print(f"[!] {data} is Not Date type.")
        sys.exit(1)

url = "http://www.wamis.go.kr:8080/wamis/openapi/wkw/we_hrdata%0A"

headers = {
    'cache-control': "no-cache",
    'postman-token': "4aafe598-6742-a796-5ab9-4e0026a0815d"
}

dt_index = pd.date_range(start='20201005', end='20201006')
querystring = {"ServiceKey": "d6ac320da07b282bde61a946be84fe99dfa71ffd19", "ResultType": "json",
                   "obscd": "10011100", "startdt": "20201005", "enddt": "20201006"}
response = requests.request("GET", url, headers=headers, params=querystring)
dat = json.loads(response.text)

df = pd.DataFrame.from_dict(dat['list'])
df.info()

df["ymdh"] = df['ymdh'].apply(convert_strtdat)
df["ta"] = df["ta"].astype(float)
df["hm"] = df["hm"].astype(float)
df["td"] = df["td"].astype(float)
df["ps"] = df["ps"].astype(float)
df["ws"] = df["ws"].astype(float)
df["wd"] = df["wd"].astype(str)
df["sihr1"] = df["sihr1"].astype(float)
df["catot"] = df["catot"].astype(float)
df["sdtot"] = df["sdtot"].astype(float)
df["sshr1"] = df["sshr1"].astype(float)

df4hours = df.set_index("ymdh").resample("4H").mean()
df4hours.to_excel('weathertime.xlsx')


2022-09-20 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.