【问题标题】:JSONDecodeError using Google Translate API with Python3JSONDecodeError 使用带有 Python3 的 Google Translate API
【发布时间】:2018-06-09 19:43:20
【问题描述】:

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

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

有人可以帮忙吗?

【问题讨论】:

  • 这里有两个错误:1. 如果p 是一个字典,str(p) 将给出一个无效的 JSON 字符串。 2.如果p是原始JSON字符串,p[:1500]会分块JSON字符串,使其成为无效的JSON字符串。
  • 感谢 Arount。奇怪的是,当我删除“p = str(p[:15000])”这一行时,我得到了完全相同的 JSON 解码器错误......关于可能发生的任何其他想法?

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在他们修复之前,您必须停止使用 googletrans,并改用 translate

    https://pypi.org/project/translate/

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2023-04-02
      • 2018-12-04
      • 2021-09-13
      • 1970-01-01
      • 2014-04-21
      • 2019-03-18
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多