百度翻译API pyhon demo
2021-02-12 19:19
# -*- coding:utf-8 -*-from http.client import HTTPConnection
import hashlib
import urllib
import randomappid = ‘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=‘+signtry:
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()
上一篇:C# 异步转同步
文章标题:百度翻译API pyhon demo
文章链接:http://soscw.com/index.php/essay/54584.html