3

我正在尝试使用“googletrans”模块将 100,000 个英语单词翻译成韩语。但经过一些迭代,它提出了

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

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

  1. 每次迭代都重新初始化 Translator(),
  2. Time-sleep(.4) 每次迭代。

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

我检查了这个模块的手册,我能找到的唯一限制是它只限制了单词的长度。但是,我试图翻译的所有单词都只是字面上的单词,而不是短语或句子。

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

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

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

2 回答 2

2

问题:我检查了这个模块的手册,我能找到的唯一限制是它只限制了单词的长度。


还有其他一些限制!

来自Googletrans 2.3.0 文档

图书馆使用注意事项

  • 由于网页版 google translate 的限制,此 API不保证该库在任何时候都能正常工作。(所以如果你不关心稳定性,请使用这个库。)
  • 如果你想使用稳定的 API,我强烈推荐你使用谷歌官方的翻译 API。
  • 如果您收到 HTTP 5xx 错误或 #6 之类的错误,可能是因为 Google 已禁止您的客户端 IP 地址。
于 2018-10-15T07:25:50.527 回答
0

该错误具有误导性,它发生是因为您因大量请求而被谷歌拒绝。我可以通过两种不同的方式解决这个问题:

  1. 使用 VPN 并更改您的 IP。它会再次起作用。
  2. 去谷歌搜索一些东西。您将收到一条消息和一个要解决的 reCAPTCHA。执行此操作后,它将再次工作一段时间。
于 2020-03-10T11:17:56.417 回答