使用Google Translate API和Python3的JSONDecodeError [英] JSONDecodeError using Google Translate API with Python3

查看:247
本文介绍了使用Google Translate API和Python3的JSONDecodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've searched thoroughly on Stack Overflow but couldn't find an answer to this problem. I'm trying to use the Google Translate API (googletrans 2.2.0) for Python (3.6.2) and am trying to translate a set of non-English documents into English. I am letting Google Translate do the language detection. Here is my code:

## 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个字符,以保持在字符数限制内.

I am throttling my call to the API by waiting 10 seconds every time. I am also only feeding the API 15k characters at a time to remain within the character limit.

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

Every time I run this code I get the following error message:

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

有人可以帮忙吗?

推荐答案

我想我可能已经找到了自己问题的答案.如果我将一次输入到API的字符数减少到5k,那么一切似乎都可以正常工作.奇怪的是,由于Googletrans文档说限制为1万5千...嗯.我将不得不分批处理请求.

I think I may have found an answer to my own question. If I reduce the number of characters I feed to the API at once to 5k, everything seems to work fine. Strange since the Googletrans documentation says that the limit is 15k... Ah well. I will have to batch the request.

这篇关于使用Google Translate API和Python3的JSONDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆