【问题标题】:429 errors returned from Google Translate when using googletrans library使用 googletrans 库时从 Google 翻译返回 429 错误
【发布时间】:2021-02-25 14:33:27
【问题描述】:

我的库 googletrans 有问题。所以我正在翻译数据框。我只是从 Google Cloud Storage 存储桶下载数据并将其存储在 DataFrames 中 -> 我选择一列进行翻译,然后就可以了。我用这个来翻译:

from googletrans import Translator, constants

def translator(text, language):
    """
    Function that receives the text we want to translate and the chosen language specified by the user and returns
    the translation
    Parameters:
    -----------
    text:
        - The string of text we want to translate
    language:
        - Specified by the user, into which language they want to translate
    """
    translate = Translator(raise_exception=True)

    # They are stored such as 'en':'english'
    def dest_language(val):
        for key, value in constants.LANGUAGES.items():
            # all values in lower case. If not we get ValueError: invalid destination language
            if value == val.lower():
                return key
        return 'Key Not Found'

    return translate.translate(text=text, dest=dest_language(language)).text

当我尝试翻译大量样本时,问题就来了。我有 9749 个样本(DataFrame 行)要翻译,每个样本或多或少有 5k 个字符,当我尝试翻译 2000is 样本时出现错误。这是我做的代码(阈值是因为我翻译不了超过5k,不知道为什么):

# Data = All DataFrame
# tr = DataFrame with the selected column
tr = Data.loc[:, [column_translation]]

# Make a threshold to divide the text
threshold = 5000

tr.reset_index(inplace=True, drop=True)
l = []
for i in range(len(tr)):
    if len(tr.loc[i,column_translation]) < threshold:
        l.append(TR(text=tr.loc[i,column_translation], language="spanish"))
        print(f"{i+1} out of {len(tr)}")

这是我收到 2000 个样本时遇到的错误:

异常:来自 ('translate.google.com',) 的意外状态代码“429” 和

我不明白我怎么会得到 429 错误,然后是 200 响应……你知道为什么会这样吗?我的意思是 HTTP 代码告诉我,我的 IP 因如此多的请求而被阻止,但我不明白为什么。

另外,使用googletrans和google-cloud-translate有什么区别?

【问题讨论】:

    标签: google-cloud-platform google-translate google-translation-api


    【解决方案1】:

    googletrans 是一个使用translate.google.com Web API 的非官方库,也与 Google 无关。
    它正在抓取翻译网页,这就是为什么您最终会收到 429 错误消息,即 HTTP 429 Too Many Request 状态代码。
    这类非官方库不稳定,最终会被封杀。

    要获得稳定的应用程序,您应该使用调用Cloud Translation APIofficial library
    Cloud Translation API 有quotas,但它会通过使用exponential backoff 重试处理这些错误。

    【讨论】:

    • 是的,但官方图书馆甚至不提供同义词、多重翻译或“字典”模式下给定翻译的置信度。使用谷歌翻译简直是优越。
    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 2018-08-27
    • 2013-08-07
    • 1970-01-01
    • 2020-08-19
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多