#/usr/bin/env python3.7 #coding=utf8 import requests import hashlib import random appid = '' #你的appid secretKey = '' #你的密钥 httpClient = None myurl = 'http://api.fanyi.baidu.com/api/trans/vip/translate' q = '今天天气不错' fromLang = 'zh' toLang = 'en' salt = random.randint(32768, 65536) sign = appid+q+str(salt)+secretKey m1 = hashlib.md5() m1.update(sign.encode("utf-8")) sign = m1.hexdigest() myurl = myurl+'?appid='+appid+'&q='+q+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign print(myurl) try: fanyi = requests.get(myurl) print(fanyi.json()) except Exception as e: print(e) finally: if httpClient: httpClient.close()