공공데이터포털 openAPI 이용하기 Python
활용신청
- 회원가입 후 개발계정 발급 신청 필요
- 오픈API 제공 데이터셋 선택
- 사용 목적 등 신청 사유 작성하여 접수. 몇 시간 걸림.
- 일반 인증키 발급
- 키 발급 신청 이후 한 시간 정도 지나야 사용 되는 듯.
- 개발 가이드 doc 보고 따라하면 되는듯
보행자무단횡단사고다발지역정보 Rest 조회 예
아래 URL을 웹 브라우저에서 조회
http://apis.data.go.kr/B552061/jaywalking/getRestJaywalking?serviceKey='YourServiceKey'&searchYearCd=2015052&siDo=11&guGun=320
조회 결과
{"searchResult":{"frequentzone":[{"fid":6104257,"grp_id":"2015052","occcd":"1132010700","spotcd":"113111001","spot":"서울 서울도봉1","spotname":"서울특별시 도봉구 창동(소피아관광호텔 부근)","occrrnc_co":10,"dthinj_co":11,"death_co":0,"serinj_co":10,"ordnr_co":1,"inj_co":0,"geometry":"{\"type\":\"Polygon\",\"coordinates\":[[[127.0377744,37.6515712],[127.0373687,37.6518466],[127.0369139,37.6520682],[127.036421,37.6522306],[127.0359023,37.6523296],[127.0353705,37.6523629],[127.0348387,37.6523296],[127.03432,37.6522306],[127.0338271,37.6520682],[127.0333723,37.6518466],[127.0329666,37.6515712],[127.0326202,37.6512486],[127.0323415,37.650887],[127.0321374,37.6504951],[127.0320129,37.6500827],[127.031971,37.6496599],[127.0320129,37.649237],[127.0321374,37.6488246],[127.0323416,37.6484328],[127.0326203,37.6480711],[127.0329668,37.6477486],[127.0333724,37.6474732],[127.0338272,37.6472516],[127.03432,37.6470892],[127.0348387,37.6469902],[127.0353705,37.646957],[127.0359023,37.6469902],[127.036421,37.6470892],[127.0369138,37.6472516],[127.0373686,37.6474732],[127.0377742,37.6477486],[127.0381207,37.6480711],[127.0383994,37.6484328],[127.0386036,37.6488246],[127.0387281,37.649237],[127.03877,37.6496599],[127.0387282,37.6500827],[127.0386036,37.6504951],[127.0383995,37.650887],[127.0381208,37.6512486],[127.0377744,37.6515712]]]}","x_crd":"127.035370503247","y_crd":"37.6496599073797"}]},"totalCount":1,"resultCode":"Success"}
Python으로 데이터 가져오기
Source Code
#!/app/python/bin/python
# openapi_Jaywalking.py
from urllib.request import urlopen
from urllib.parse import urlencode,unquote,quote_plus
import urllib
url = 'http://apis.data.go.kr/B552061/jaywalking/getRestJaywalking'
queryParams = '?' + urlencode({ quote_plus('servicekey') : 'YourServiceKey',
quote_plus('LAYERS') : 'frejaywalking',
quote_plus('FORMAT') : 'image/png',
quote_plus('TRANSPARENT') : 'true',
quote_plus('SERVICE') : 'WMS',
quote_plus('VERSION') : '1.1.1',
quote_plus('REQUEST') : 'GetMap',
quote_plus('SRS') : 'EPSG:900913',
quote_plus('BBOX') : '14142684.718103,4505504.1936344,14147576.687913,4510396.1634438',
quote_plus('width') : '2024',
quote_plus('height') : '1838',
quote_plus('srs') : 'EPSG:900913',
quote_plus('searchYearCd') : '2015052',
quote_plus('siDo') : '11',
quote_plus('guGun') : '320' })
request = urllib.request.Request(url+unquote(queryParams))
print ('Your Request:\n'+url+queryParams)
request.get_method = lambda: 'GET'
response_body = urlopen(request).read()
print ("\nResult:")
print (response_body)
print ("\nDataType of Result Data:")
print (type(response_body))
urllib.request
- urlopen
urllib.parse
- urlencode
- unquote
- quote_plus
Result
Your Request:
http://apis.data.go.kr/B552061/jaywalking/getRestJaywalking?servicekey=YourServiceKey&searchYearCd=2015052&siDo=11&guGun=320
Result:
b'{"searchResult":{"frequentzone":[{"fid":6104257,"grp_id":"2015052","occcd":"1132010700","spotcd":"113111001","spot":"\xec\x84\x9c\xec\x9a\xb8 \xec\x84\x9c\xec\x9a\xb8\xeb\x8f\x84\xeb\xb4\x891","spotname":"\xec\x84\x9c\xec\x9a\xb8\xed\x8a\xb9\xeb\xb3\x84\xec\x8b\x9c \xeb\x8f\x84\xeb\xb4\x89\xea\xb5\xac \xec\xb0\xbd\xeb\x8f\x99(\xec\x86\x8c\xed\x94\xbc\xec\x95\x84\xea\xb4\x80\xea\xb4\x91\xed\x98\xb8\xed\x85\x94 \xeb\xb6\x80\xea\xb7\xbc)","occrrnc_co":10,"dthinj_co":11,"death_co":0,"serinj_co":10,"ordnr_co":1,"inj_co":0,"geometry":"{\\"type\\":\\"Polygon\\",\\"coordinates\\":[[[127.0377744,37.6515712],[127.0373687,37.6518466],[127.0369139,37.6520682],[127.036421,37.6522306],[127.0359023,37.6523296],[127.0353705,37.6523629],[127.0348387,37.6523296],[127.03432,37.6522306],[127.0338271,37.6520682],[127.0333723,37.6518466],[127.0329666,37.6515712],[127.0326202,37.6512486],[127.0323415,37.650887],[127.0321374,37.6504951],[127.0320129,37.6500827],[127.031971,37.6496599],[127.0320129,37.649237],[127.0321374,37.6488246],[127.0323416,37.6484328],[127.0326203,37.6480711],[127.0329668,37.6477486],[127.0333724,37.6474732],[127.0338272,37.6472516],[127.03432,37.6470892],[127.0348387,37.6469902],[127.0353705,37.646957],[127.0359023,37.6469902],[127.036421,37.6470892],[127.0369138,37.6472516],[127.0373686,37.6474732],[127.0377742,37.6477486],[127.0381207,37.6480711],[127.0383994,37.6484328],[127.0386036,37.6488246],[127.0387281,37.649237],[127.03877,37.6496599],[127.0387282,37.6500827],[127.0386036,37.6504951],[127.0383995,37.650887],[127.0381208,37.6512486],[127.0377744,37.6515712]]]}","x_crd":"127.035370503247","y_crd":"37.6496599073797"}]},"totalCount":1,"resultCode":"Success"}'
DataType of Result Data:
<class 'bytes'>