python - JSONDecodeError 在 Python3 中使用谷歌翻译 API

标签 python python-3.x google-translate language-translation jsondecoder

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

## 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)

我通过每次等待 10 秒来限制对 API 的调用。我也一次只向 API 提供 15k 个字符以保持在字符数限制内。

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

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

有人可以帮忙吗?

最佳答案

我想我可能已经找到了我自己问题的答案。如果我将一次提供给 API 的字符数减少到 5k,一切似乎都正常。很奇怪,因为 Googletrans 文档说限制是 15k...嗯。我将不得不对请求进行批处理。

关于python - JSONDecodeError 在 Python3 中使用谷歌翻译 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021371/

相关文章:

Python 百分比格式字符串,% 之前有空格

python - IPython 自动完成调用 __getattr__

python - Matplotlib:给Figure.add_subplot()的3位参数的含义是什么?

python - 从简单的 pandas 数据帧创建矩阵

php - 访问谷歌翻译时重定向到验证码

c - 谷歌翻译与c

python - 相同域、不同端口的 CherryPy session

python - Celery 任务获取 SoftTimeLimitExceeded 调用 API

python - 如何在列中的每个不同值上拆分 DataFrame?

javascript - 使用付费帐户的谷歌 tts