百度翻译api的使用(python)

百度翻译api申请:
https://api.fanyi.baidu.com/

python代码:

import requests
import time
import hashlib
import json


api_url = "http://api.fanyi.baidu.com/api/trans/vip/translate"  
my_appid = ''  #你的APP ID
cyber = ''  #你的秘钥

def requests_for_dst(word):  
    #init salt and final_sign  
    salt = str(time.time())[:10]  
    final_sign = str(my_appid)+word+salt+cyber  
    final_sign = hashlib.md5(final_sign.encode("utf-8")).hexdigest()  
    #区别en,zh构造请求参数  
    paramas = {  
            'q':word,  
            'from':'en',  # 源语言
            'to':'zh',   # 目标语言
            'appid':my_appid,  
            'salt':'%s'%salt,  
            'sign':'%s'%final_sign  
            }  #翻译请求参数

    response = requests.get(api_url,params = paramas).content  
    content = str(response,encoding = "utf-8")  
    json_reads = json.loads(content)  
    return json_reads['trans_result'][0]['src'],json_reads['trans_result'][0]['dst']



src,dst=requests_for_dst('We chose GoogLeNet as our team-name in the ILSVRC14 competition. This name is an homage to Yann LeCuns pioneering LeNet 5 network [10]. We also use GoogLeNet to refer to the particular incarnation of the Inception architecture used in our submission for the competition. We have also used a deeper and wider Inception network, the quality of which was slightly inferior, but adding it to the ensemble seemed to improve the results marginally. ')
print(src)
print(dst)

原文出处:/show_1EF5X2Qma67c.html