百度翻译API的python调用
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "leo"
__time__ = "2018-08-03"
import requests
import json
import hashlib
def md5_sign(s):
return hashlib.md5(s.encode('utf8')).hexdigest()
def baidu_fanyi(q):
url = "http://api.fanyi.baidu.com/api/trans/vip/translate"
f = 'en'
to = 'zh'
appid = 'xxx' # 自己申请
salt = '1435660288'
key = 'xxx' # 自己申请
finial_str = '%s%s%s%s' % (appid, q, salt, key)
sign = md5_sign(finial_str)
params = {
'q': q,
'from': f,
'to': to,
'appid': appid,
'salt': salt,
'sign': sign,
}
res_json = requests.get(url, params=params).json()
res = res_json['trans_result'][0]["dst"]
print(res)
if __name__ == '__main__':
baidu_fanyi('apple')
网友评论