python 调用Google Translate API 翻译

来源:互联网 发布:淘宝客申请高佣金api 编辑:程序博客网 时间:2024/05/21 11:13

1、安装相关模块

pip install --upgrade google-cloud

或者

pip install --upgrade google-cloud-translate


2、调用api进行翻译之前,需要安装相关证书

    官网提供的方法有三种:1、安装  Google Cloud SDK 下载地址

                                                2、running in Google App Engine production

                              3、running in Google Compute Engine production

因为在本地上运行,使用的第一种方式,安装之后会有自动弹出验证邮箱的界面,如果没有在Google cloud sdk shell  运行gcloud auth application-default login

3、编写Python 程序

from google.cloud import translatetranslate_client = translate.Client()text = u'hello,world'target = 'ru'translation = translate_client.translate(text,target_language=target)print u'Text:{}'.format(text)print u'translation:{}'.format(translation['translatedText'])

如果想要翻译成中文 把target =‘ru’ 改成 target = 'zh-CN'

转载请注明 :http://blog.csdn.net/u010856630/article/details/73810718