흰 스타렉스에서 내가 내리지

api 가져오기 본문

Python

api 가져오기

주씨. 2022. 6. 9. 15:32
728x90
import urllib.request
import json


client_id = "jqMnVOuFgx8MmsSGkkCP"
client_secret = "NjSdsP3A"
encText = urllib.parse.quote("기생충")
url = "https://openapi.naver.com/v1/search/movie.json?query=" + encText # json 결과
# url = "https://openapi.naver.com/v1/search/blog.xml?query=" + encText # xml 결과
request = urllib.request.Request(url)
request.add_header("X-Naver-Client-Id",client_id)
request.add_header("X-Naver-Client-Secret",client_secret)
response = urllib.request.urlopen(request)
rescode = response.getcode()
if(rescode==200):
    response_body = response.read()
    # print(response_body.decode('utf-8'))
else:
    print("Error Code:" + rescode)
    

resdata = response_body.decode('utf-8')

# with open('movie.json', 'w', encoding='UTF-8-sig') as file:
#     file.write(json.dumps(resdata, ensure_ascii=False))
    
    
pydata = json.loads(resdata)
data = pydata['items']

print(data[0]['title'])