파이썬으로 티맵API json결과인 딕셔너리 값을 가져오려고하는데 잘 안되네요
조회수 1521회
안녕하세요?
아나콘다를 사용해서 티맵 API를 파이썬에서 json의 값을 request로 가져오고 그게 딕셔너리 형태로 저장이 되었더라고요. 여기서 원하는 값이 두개인데 그게 잘 가져와지지 않네요. 무엇을 잘못 입력했을까요?
제가 가져 오고 싶은 데이터는 totalDistance
와 totalTime
이 두가지 입니다.
혹시 제가 딕셔너리 구조를 잘못 파악하고 있나 싶어서 일단 데이터 프레임으로 바꿔서 엑셀로 저장 해봤습니다.
그랬더니
type는
FeatureCollection
feautres에는 {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [14395473.264624588, 4237025.082804387]}, 'properties': {'totalDistance': 6606, 'totalTime': 879, 'totalFare': 0, 'taxiFare': 7400, 'index': 0, 'pointIndex': 0, 'name': '', 'description': '돋질로92번길 을 따라 방면으로 30m 이동', 'nextRoadName': '돋질로92번길', 'turnType': 200, 'pointType': 'S'}}
이렇게 나오고요
저는 이렇게 입력하면 나올줄 알았는데 잘못 알았나보네요
a['FeatureCollection']['totalDistance']
어떻게 하면 저 값을 꺼낼수 있을까요?
이하 코드입니다
import requests
import logging
logging.basicConfig(format='[%(levelname)s] %(asctime)s - %(message)s', level=logging.INFO, datefmt="%H:%M:%S")
version = 1
callback = "result"
apiUrl = f"https://apis.openapi.sk.com/tmap/routes?version={version}&callback={callback}"
headers = {
"appKey": "----",
"Accept": "application/json",
"Content-Type": "application/json; charset=UTF-8"
}
json = {
"startX" : "129.3165257",
"startY" : "35.536378",
"endX" : "129.3268071",
"endY" : "35.5265278",
"reqCoordType" : "WGS84GEO",
"resCoordType" : "EPSG3857"
# "searchOption" : searchOption,
# "trafficInfo" : trafficInfochk
}
req = requests.post(apiUrl, headers=headers, json=json)
logging.info(req.json())
1 답변
-
j = req.json() for feat in j["features"]: logging.info(feat) feat = j["features"][0] dist = feat["properties"]["totalDistance"] time = feat["properties"]["totalTime"] logging.info(f"totalDistance = {dist}, totalTime = {time}")
댓글 입력