본문으로 바로가기
 

공공데이터 포털

국가에서 보유하고 있는 다양한 데이터를『공공데이터의 제공 및 이용 활성화에 관한 법률(제11956호)』에 따라 개방하여 국민들이 보다 쉽고 용이하게 공유•활용할 수 있도록 공공데이터(Datase

www.data.go.kr

설명

코로나19 관련된 해외 운항, 입국, 코로나19 관련 등의 국가·지역별 최신안전소식을 제공합니다.

요청시 최신의 정보만을 제공합니다.

 

https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15043145

 

데이터 구조

content : 컬럼이 없는 항목도 있습니다. 이 경우에는 해당 국가에서 메시지를 준비? 하지 않은 것으로 보입니다.

totalCount : 총 항목의 수를 의미

numOfRows : 한 페이지당 보여줄 항목의 수

pageNo : 페이지

 

*

totalCount가 84개 이고 numOfRows가 10이면 pageNo를 1부터 9까지 9번 요청하면 됩니다.

numOfRows를 100정도 pageNo를 1로 해서 요청하면, 한페이지에 모든 항목이 들어갑니다.

 

브라우저 요청 결과입니다.

 

content가 없는 국가의 항목들

 

한번에 호출

요청하기 전까지는 totalCount 를 알 수 없습니다.

임의의 큰 수로 요청하시면 됩니다. 

 

코드

발급 받으신 키값만 수정하시면 됩니다.

응답 데이터에 대해서 xml 파일로 쓰며 브라우저로 띄워줍니다.

'''
외교부_국가·지역별 최신안전소식(코로나관련)
'''
import webbrowser
import requests
from urllib import parse
import xml.dom.minidom

url = 'http://apis.data.go.kr/1262000/SafetyNewsList/getCountrySafetyNewsList'
key = 'Input Your Key'

queryParams = f'?{parse.quote_plus("ServiceKey")}={key}&' + parse.urlencode({ 
    parse.quote_plus('numOfRows') : '500', 
    parse.quote_plus('pageNo') : '1', 
    parse.quote_plus('title1') : '입국', 
    parse.quote_plus('title2') : '코로나', 
    parse.quote_plus('title3') : '운항', 
    parse.quote_plus('title4') : '항공권', 
    parse.quote_plus('title5') : '격리'
    })

res = requests.get(url + queryParams)
webbrowser.open(res.request.url)

xml = xml.dom.minidom.parseString(res.text)
pretty_xml = xml.toprettyxml()
with open('result.xml', 'w', encoding='utf-8') as f:
    f.write(pretty_xml)

result.xml
0.19MB

 

직관적으로는 챗봇으로 활용하고 싶네요.