百度翻译API python3 demo

目前从百度翻译下载来的是 python2的

我把pyhon3 的贴出来.

# -*- coding:utf-8 -*-

from http.client import HTTPConnection
import hashlib
import urllib
import random

appid = '20151113000005349'
secretKey = 'osubCEzlGjzvw8qdQc41'


httpClient = None
myurl = '/api/trans/vip/translate'
q = 'apple'
fromLang = 'en'
toLang = 'zh'
salt = random.randint(32768, 65536)

sign = appid+q+str(salt)+secretKey
# m1 = md5.new()
m1 = hashlib.md5()
m1.update(sign.encode())
sign = m1.hexdigest()
myurl = myurl+'?appid='+appid+'&q='+urllib.parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign

try:
httpClient = HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)

#response是HTTPResponse对象
response = httpClient.getresponse()
print (response.read())
except Exception as e:
print (e)

finally:
if httpClient:
httpClient.close()
原文地址:https://www.cnblogs.com/notfresh/p/8468646.html