def printError(qq):
import http.client
import hashlib
from urllib import parse
import random
import json
appid = '' # 填写你的appid 这两个变量需要你去百度翻译开发者去申请账号免费的
secretKey = '' # 填写你的密钥
httpClient = None
myurl = '/api/trans/vip/translate'
fromLang = 'auto' #原文语种
toLang = 'zh' #译文语种
salt = random.randint(32768, 65536)
q=str(qq)
sign = appid + str(q) + str(salt) + secretKey
sign = hashlib.md5(sign.encode()).hexdigest()
#myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(salt) + '&sign=' + sign
myurl = myurl + '?appid=' + appid + '&q=' + parse.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(salt) + '&sign=' + sign
try:
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
# response是HTTPResponse对象
response = httpClient.getresponse()
result_all = response.read().decode("utf-8")
result = json.loads(result_all)
# print(type(result))
#print (result)
#修改这里 可以调整输出结果
# print (type(result['trans_result']))
print ('Error:'+result['trans_result'][0]['src'],end=': ')
print (result['trans_result'][0]['dst'])
except Exception as e:
print (e)
finally:
if httpClient:
httpClient.close()