当前位置:   article > 正文

使用python语言调用有道翻译接口实现中英互译_有道翻译appkey

有道翻译appkey

中英互译函数:

  1. def translate_english_to_chinese(text):
  2. """英文翻译为中文."""
  3. app_key = "xxxxx" # 有道翻译App Key
  4. app_secret = "xxxxxxx" # 有道翻译App Secret
  5. base_url = "https://openapi.youdao.com/api"
  6. app_id = "xxxxxxxx" # 有道翻译App ID
  7. from_lang = "en"
  8. to_lang = "zh-CHS"
  9. salt = str(random.randint(1, 65536))
  10. sign_str = app_id + text + salt + app_secret
  11. sign = hashlib.md5(sign_str.encode()).hexdigest()
  12. params = {
  13. "q": text,
  14. "from": from_lang,
  15. "to": to_lang,
  16. "appKey": app_key,
  17. "salt": salt,
  18. "sign": sign,
  19. }
  20. for the_num in range(5):
  21. try:
  22. response = req.post(base_url, data=params)
  23. result = response.json()
  24. if "translation" in result:
  25. return result["translation"][0]
  26. else:
  27. print("Translation Error:", result.get("errorMessage", "Unknown error"))
  28. time.sleep(the_num)
  29. except Exception as exception:
  30. print("Translation Error:", exception)
  31. time.sleep(the_num)
  32. return ''
'
运行

调用函数:

  1. if __name__ == '__main__':
  2. # content = ''
  3. # for the_content in content_list:
  4. # content = f'{content}{translate_english_to_chinese(the_content)}'
  5. # time.sleep(0.02)
  6. word = 'i like apple'
  7. new_result = translate(word)
  8. print(new_result)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/855629
推荐阅读
相关标签
  

闽ICP备14008679号