공공데이터포털 Data API

공공데이터 포털 open API 테스트

#공공 open API 테스트
import requests
from bs4 import BeautifulSoup

f = open('./data_go_key.txt')
key = f.readlines()
decKey = key[0].strip()
encKey = key[1].strip()

import requests

# 미세먼지 검색 후 활용 신청 주소
url = 'http://apis.data.go.kr/B552584/UlfptcaAlarmInqireSvc/getUlfptcaAlarmInfo'
params_xml ={'serviceKey' : decKey, 'returnType' : 'xml', 'numOfRows' : '100', 'pageNo' : '1', 'year' : '2023', 'itemCode' : 'PM10' }
params_json = {'serviceKey' : decKey, 'returnType' : 'json', 'numOfRows' : '100', 'pageNo' : '1', 'year' : '2023', 'itemCode' : 'PM10' }

XML로 호출

# xml로 호출
resp_xml = requests.get(url, params=params_xml)
if resp_xml.status_code == 200:
    xml = BeautifulSoup(resp_xml.text)

JSON으로 호출

# json으로 호출
resp_json = requests.get(url, params=params_json)
if resp_xml.status_code == 200:
    json = resp_json.json()

viewer로 확인 위해 file로 저장

# viewer로 확인 위해 file로 저장
with open('air_xml.txt', 'w', encoding='utf-8') as f:
    f.write(resp_xml.text)

with open('air_json.txt', 'w', encoding='utf-8') as f:
    f.write(resp_json.text)

처리할 데이터 값

# 처리할 데이터 값 
{
    발령지역:districtName
    발령일:issueDate,
    발령 시간:issueTime,
    경보단계:issueGbn,
    발령농도:issueVal,
    해제농도:clearVal,
    해제일:clearDate, 
    해제 시간:clearTime
}

XML 처리

# 방법 1
districtName = [i.text for i in xml('districtname')]
issueDate = [i.text for i in xml('issuedate')]
issueTime = [i.text for i in xml('issuetime')]
issueGbn = [i.text for i in xml('issuegbn')]
issueVal = [i.text for i in xml('issueval')]
clearVal = [i.text for i in xml('clearval')]
clearDate = [i.text for i in xml('cleardate')]
clearTime = [i.text for i in xml('cleartime')]

datas = [districtName, issueDate, issueTime, issueGbn, issueVal, clearVal, clearDate, clearTime]
for i in datas:
    print(len(i))
# 방법 2
data_names = ['districtname','issuedate','issuetime','issuegbn','issueval','clearval','cleardate','cleartime']
datas = []
for n in data_names:
    rs = [i.text for i in xml(n)]
    datas.append(rs)

데이터 개수 검산

# 데이터 개수 검산
for i in datas:
    print(len(i))

최종 데이터 머지 및 확인

datas_fin = list(zip(districtName, issueDate, issueTime, issueGbn, issueVal, clearVal, clearDate, clearTime))
print(datas_fin)

JSON 처리

jsons = json['response']['body']['items']
print(len(jsons))

districtName = []
issueDate = []
issueTime = []
issueGbn = []
issueVal = []
clearVal = []
clearDate = []
clearTime = []
for i in jsons:
    districtName.append(i['districtName'])
    issueDate.append(i['issueDate'])
    issueTime.append(i['issueTime'])
    issueGbn.append(i['issueGbn'])
    issueVal.append(i['issueVal'])
    clearVal.append(i['clearVal'])
    clearDate.append(i['clearDate'])
    clearTime.append(i['clearTime'])

items = [districtName, issueDate, issueTime, issueGbn, issueVal, clearVal, clearDate, clearTime]

데이터 개수 검산

# 데이터 개수 검산
for i in items:
    print(len(i))

최종 데이터 머지 및 확인

items_fin = list(zip(districtName, issueDate, issueTime, issueGbn, issueVal, clearVal, clearDate, clearTime))
print(items_fin)