abc333
import os
import sys
import urllib.request
client_id = "QTKf81BA4QcR0mz_e5tc"
client_secret = "Wbvq8_eYhh"
encText = urllib.parse.quote("학교에 가고 싶다.")
data = "source=ko&target=en&text=" + encText
url = "https://openapi.naver.com/v1/language/translate"
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, data=data.encode("utf-8"))
rescode = response.getcode()
if(rescode==200):
response_body = response.read()
print(response_body.decode('utf-8'))
else:
print("Error Code:" + rescode)
{"message":{"@type":"response","@service":"naverservice.labs.api","@version":"1.0.0","result":{"translatedText":"I want to go to school."}}}
type(response_body)
bytes
import json
result2=json.loads(response_body)
result2
{'message': {'@service': 'naverservice.labs.api',
'@type': 'response',
'@version': '1.0.0',
'result': {'translatedText': 'I want to go to school.'}}}
type(result2)
dict
print(result2['message'])
{'@type': 'response', '@service': 'naverservice.labs.api', '@version': '1.0.0', 'result': {'translatedText': 'I want to go to school.'}}
print(result2['message']['result']['translatedText'])
I want to go to school.
Leave a Comment