JSONDecodeError:使用googletrans模块

时间:2018-10-15 02:34:41

标签: python json google-translation-api

我正在尝试使用“ googletrans”模块将100,000个英语单词翻译成韩语。但是经过一番迭代,它会引发

  

“ JSONDecodeError:预期值:第1行第1列(字符0)”。

我试图弄清楚,但是网络上的解决方案对我不起作用。
我尝试过的是

  1. 每次迭代都重新初始化Translator()
  2. 每个迭代的时间睡眠(.4)。

最奇怪的部分是它随机发生。有时经过几百次迭代后会升高,有时会经过几遍迭代。

我检查了该模块的手册,发现的唯一限制是它仅限制了单词的长度。但是,我要翻译的所有单词都是字面上的单词,而不是短语或句子。

key_list = list(senticnet.keys())
for key in key_list:
  translator = Translator()
  time.sleep(.4)
  print(translator.translate(key, dest='ko'))

在上面的代码中,sendicnet是一个字典变量。看起来像

senticnet['abusive_conduct'] = ['0', '0', '0.853', '-0.84', '#anger', '#disgust', 'negative', '-0.84', 'flagrant', 'cry', 'glaring', 'gross', 'rank']
senticnet['abusive_father'] = ['0', '0', '0.821', '-0.95', '#anger', '#disgust', 'negative', '-0.88', 'student', 'serious_student', 'addiction', 'graduate_student', 'hard_worker']

这是错误消息

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/client.py in translate(self, text, dest, src)
170 
171         origin = text
--> 172         data = self._translate(text, dest, src)
173 
174         # this code will be updated when the format is changed.

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/client.py in _translate(self, text, dest, src)
 79         r = self.session.get(url, params=params)
 80 
---> 81         data = utils.format_json(r.text)
 82         return data
 83 

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/utils.py in format_json(original)
 60         converted = json.loads(original)
 61     except ValueError:
---> 62         converted = legacy_format_json(original)
 63 
 64     return converted

/content/gdrive/My Drive/Colab Notebooks/py-googletrans/googletrans/utils.py in legacy_format_json(original)
 52             text = text[:p] + states[j][1] + text[nxt:]
 53 
---> 54     converted = json.loads(text)
 55     return converted
 56 

/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
352             parse_int is None and parse_float is None and
353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
355     if cls is None:
356         cls = JSONDecoder

/usr/lib/python3.6/json/decoder.py in decode(self, s, _w)
337 
338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
340         end = _w(s, end).end()
341         if end != len(s):

/usr/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
355             obj, end = self.scan_once(s, idx)
356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

2 个答案:

答案 0 :(得分:2)

  

问题:我检查了该模块的手册,发现的唯一限制是它仅限制了单词的长度。


还有其他一些限制!

  

来自Googletrans 2.3.0 documentation

     

关于图书馆的使用情况

     
      
  • 由于Google翻译的网络版本的局限性,该API 不能保证该库在任何时候都能正常工作。 (因此,如果您不关心稳定性,请使用此库。)
  •   
  • 如果您想使用稳定的API,我强烈建议您使用Google的官方翻译API。
  •   
  • 如果您收到HTTP 5xx错误或类似#6的错误,则可能是因为Google禁止了您的客户端IP地址。
  •   

答案 1 :(得分:0)

该错误具有误导性,它的发生是因为您因众多请求而被Google拒绝。我可以通过两种不同的方式解决此问题:

  1. 使用VPN并更改您的IP。它将再次工作。
  2. 转到Google并搜索一些内容。您将收到一条消息和一个reCAPTCHA来解决。完成此操作后,它将再次工作一段时间。
相关问题