def youdaotranslate(list_str,APP_KEY,APP_SECRET): httpClient = None myurl = '/api/trans/vip/translate' fromLang = 'zh' # 原文语种 toLang = 'vie' # 译文语种 salt = random.randint(32768, 65536) sign = APP_KEY + list_str + str(salt) + APP_SECRET sign = hashlib.md5(sign.encode()).hexdigest() myurl = myurl + '?appid=' + APP_KEY + '&q=' + urllib.parse.quote(list_str) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(salt) + '&sign=' + sign try: httpClient = http.client.HTTPConnection('api.fanyi.baidu.com') httpClient.request('GET', myurl) # response是HTTPResponse对象 response = httpClient.getresponse() result_all = response.read().decode("utf-8") result = json.loads(result_all) zidian = []#返回字典类型 for v in result['trans_result']: zidian.append(v['dst']) return zidian except Exception as e: print(e) finally: if httpClient: httpClient.close()
python 百度翻译接口调用
最新推荐文章于 2024-08-15 01:55:01 发布