使用Google Translate API和Python3的JSONDecodeError

Roa*_*ing 6 python google-translate python-3.x language-translation jsondecoder

我已经在Stack Overflow上进行了彻底的搜索,但是找不到此问题的答案。我正在尝试使用适用于Python(3.6.2)的Google Translate API(googletrans 2.2.0),并试图将一组非英语文档翻译成英语。我让Google Translate进行语言检测。这是我的代码:

## newcorpus is a corpus I have created consisting of non-english documents
fileids = newcorpus.fileids
for f in fileids:
    p = newcorpus.raw(f) 
    p = str(p[:15000])
    translated_text = translator.translate(p)
    print(translated_text)
    sleep(10)
Run Code Online (Sandbox Code Playgroud)

我每次都要等待10秒,从而限制了对API的调用。我也一次只喂API 15k个字符,以保持在字符数限制内。

每次运行此代码,都会收到以下错误消息:

## newcorpus is a corpus I have created consisting of non-english documents
fileids = newcorpus.fileids
for f in fileids:
    p = newcorpus.raw(f) 
    p = str(p[:15000])
    translated_text = translator.translate(p)
    print(translated_text)
    sleep(10)
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Roa*_*ing 4

我想我可能已经找到了我自己问题的答案。如果我将一次提供给 API 的字符数减少到 5k,那么一切似乎都工作正常。奇怪的是,Googletrans 文档说限制是 15k...啊好吧。我将不得不批量处理该请求。