postman&API
Postman naver API 내용을 Python으로 코딩해보기.
몽자비루
2024. 6. 7. 16:34
- GET API - python으로 만들어보기
참고 링크 : https://rusharp.tistory.com/79- 코드 내용
from naver_api_token import * import requests import pprint url = "https://openapi.naver.com/v1/search//local.json" # 헤더 설정 headers = { "X-Naver-Client-Id": client_id, "X-Naver-Client-Secret": client_secret, } # 파라미터값 지정 params = { "query": "seoul" } # requests.get을 통해 GET 요청받음 response = requests.get(url, headers=headers, params=params) # 코드번호가 200이면 결과값 출력. # 아니면 에러코드번호 출력 if(response.status_code==200): response_body = response.json() pprint.pprint(response_body) else: print("Error Code:" + response.status_code)
- 출력 결과
- 코드 내용
- POST API - python으로 만들어보기
참고 링크 :https://rusharp.tistory.com/80- 코드 내용
import pprint from naver_api_token import * import requests import json # 그룹으로 묶은 검색어에 대한 네이버 통합검색에서 검색 추이 데이터를 반환. url = "https://openapi.naver.com/v1/datalab/shopping/categories" # 헤더 설정 headers = { "X-Naver-Client-Id": client_id, "X-Naver-Client-Secret": client_secret, "Content-Type": "application/json" } # 요청 본문 데이터 설정 body = { "startDate": "2024-01-01", "endDate": "2024-06-03", "timeUnit": "month", "category": [ {"name": "노트북", "param": ["50000151"]}, {"name": "PC", "param": ["50000089"]} ], "device": "pc", "gender": "f", "ages": ["20", "30"] } # POST 요청 보내기 # json.dumps(body) 는 body dictionary를 JSON 형식의 문자열로 변환함. response = requests.post(url, headers=headers, data=json.dumps(body)) # 코드번호가 200이면 결과값 출력. # 아니면 에러코드번호 출력 if(response.status_code==200): response_body = response.json() pprint.pprint(response_body) else: print("Error Code:" + response.status_code)
- 출력 결과
- 코드 내용