Skip to main content
 首页 » 编程设计

python之Google Translate API之错误代码 500 后端错误

2024年10月17日9kerrycode

我正在通过 Google API 的 Python 模块向 Google Translate API 提交各种查询。有时我会收到以下错误消息:

from googleapiclient.discovery import build 
 
service = build('translate', 'v2', developerKey='my_key') 
translation = service.translations().list( 
                    source='zh', 
                    target='en', 
                    q=text_for_translation).execute() 

HttpError: HttpError 500 请求 https://www.googleapis.com/language/translate/v2?q=%E7%B7%9A&source=zh&alt=json&target=en&key=my_key 返回“后端错误”>

知道为什么会发生这种情况以及如何避免这种情况吗?我找不到关于这个特定问题的任何信息。

我似乎随机得到错误,因为几乎所有我提交的翻译查询都没有任何问题地返回。此外,如果我重新提交完全相同的查询,它将成功返回。

请您参考如下方法:

来自谷歌 API 常见问题页面:

This could happen if you submit text without the source language specified. If Google cannot determine the source language given the text (usually because the text is too short), we will return a 500. This situation can be fixed by specifying the source language explicitly.

更多信息及其他信息Google translate API