GoogleTrans API 错误 - 预期值:第 1 行第 1 列(字符 0)

新手上路,请多包涵

在迭代中翻译数千个文本数据时出现此错误:

 Expecting value: line 1 column 1 (char 0)

我翻译大量文本的代码:

 translatedList = []
for index, row in df.iterrows():
    newrow = copy.deepcopy(row)
    try:
        # translate the 'text' column
        translated = translator.translate(row['text'], dest='en')
        newrow['translated'] = translated.text
    except Exception as e:
        print(str(e))
        continue
    translatedList.append(newrow)

我在翻译大约 2-3k 行后收到此错误。

原文由 Kerem 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 650
2 个回答

我有点想通了这个问题。我认为这是关于 Google API 的请求限制。

我通过在每次迭代时重新初始化翻译器 API 解决了这个问题:

 import copy
from googletrans import Translator

translatedList = []
for index, row in df.iterrows():
    # REINITIALIZE THE API
    translator = Translator()
    newrow = copy.deepcopy(row)
    try:
        # translate the 'text' column
        translated = translator.translate(row['text'], dest='en')
        newrow['translated'] = translated.text
    except Exception as e:
        print(str(e))
        continue
    translatedList.append(newrow)

原文由 Kerem 发布,翻译遵循 CC BY-SA 4.0 许可协议

这就是我必须做的来绕过他们的 API 调用限制……我使用 VPN,特别是 Nord-Vpn,所以要按照我的方式来做,你需要能够通过 VPN 连接/断开连接终点站…

     def translate_text(text, dest_language="en"):
        # Used to translate using the googletrans library
        import json
        translator = googletrans.Translator()
        try:
            translation = translator.translate(text=text, dest=dest_language)
        except json.decoder.JSONDecodeError:
            # api call restriction
            process = subprocess.Popen(["nordvpn", "d"], stdout=subprocess.PIPE)
            process.wait()
            process = subprocess.Popen(["nordvpn", "c", "canada"], stdout=subprocess.PIPE)
            process.wait()
            return Process_Data.translate_text(text=text, dest_language=dest_language)
        return translation

原文由 Austin Marino 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题