오픈 API 데이터 에러 오류
조회수 614회
안녕하세요. 기상청 날씨 데이터를 공부하는중에 이번에는 국가수자원 종합관리시스템 (WAMIS)에 있는 데이터를 불러와 엑셀로 저장하려고 합니다. 그런데 원인 모를 오류가 생겨서 구글링해도 혼자 힘으로 해결이 안되어 도움 요청드립니다.
해당 코드는 이렇습니다.
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()
print(df4hours)
df4hours.to_excel('weathertime.xlsx')
참고사이트 : http://www.wamis.go.kr:8080/wamisweb/we/w12.do
해당 코드는 pandas를 사용하여 년월일로 묶어 4시간단위로 출력하는 날씨정보 코드를 엑셀로 담으려고합니다. 그런데 여기서 dat.extend(response.json()["result"]["data"]) 이 구문에 data가 키에러라고 뜨는데 어디서부터가 잘못되었는지 모르겠네요..
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
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')
- 정말 감사드립니다 .ㅠㅠ 알 수 없는 사용자 2020.10.7 15:27
- 혹시 국가수데이터말고 기상청 데이터를 짜주신 코딩에 응용해봤는데 코드에러가 나네용 한번 봐주실수 있으신가요?.. 알 수 없는 사용자 2020.10.12 17:20
- 제가 답변드린거처럼 해당 api에서 제공되는 데이터 포맷을 맞추듯이 기상청데이터에 포맷을 맞추세요 김호원 2020.10.13 10:21
댓글 입력