■ HTTPSConnection 클래스의 request/getresponse 메소드를 사용해 NAVER 도서 정보를 검색하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import http.client import urllib.request import xml.dom.minidom naverClientID = "XXXXXXXXXXXXXXXXXXXX" naverClientSecret = "XXXXXXXXXX" # openAPI가 https 프로토콜을 사용하기 때문에 HTTPSConnection을 사용한다. httpsConnection = http.client.HTTPSConnection("openapi.naver.com") #httpsConnection.set_debuglevel(1) # 디버그 모드를 설정한다. headerDictionary = {"X-Naver-Client-Id" : naverClientID, "X-Naver-Client-Secret" : naverClientSecret} urlEncodedSearchBookName = urllib.parse.quote("파이썬") queryString = "?query=" + urlEncodedSearchBookName + "&display=10&start=1" httpsConnection.request("GET", "/v1/search/book.xml" + queryString, None, headerDictionary) httpResponse = httpsConnection.getresponse() if int(httpResponse.status) == 200: print(xml.dom.minidom.parseString(httpResponse.read().decode("utf-8")).toprettyxml()) else: print("HTTP Request is failed :" + httpResponse.reason) print(httpResponse.read().decode("utf-8")) httpsConnection.close() |