JSONDecodeError: using googletrans module
JSONDecodeError: using googletrans module
我正在尝试使用 'googletrans' 模块将 100,000 个英语单词翻译成韩语。但经过一些迭代后,它引发了
'JSONDecodeError: Expecting value: line 1 column 1 (char 0)'.
我想弄明白,但网络上的解决方案对我不起作用。
我试过的是
- 每次迭代都重新初始化 Translator(),
- 每次迭代的睡眠时间 (.4)。
最奇怪的是它是随机发生的。有时它会在几百次迭代后出现,有时会在几次迭代后出现。
我查了这个模块的手册,我能找到的唯一限制是它只限制了单词的长度。但是,我要翻译的所有单词都只是字面上的单词,而不是短语或句子。
key_list = list(senticnet.keys())
for key in key_list:
translator = Translator()
time.sleep(.4)
print(translator.translate(key, dest='ko'))
上面代码中,senticnet是一个字典变量。看起来像
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)
Question: I checked manual of this module and the only limit I could find was that it only limits the length of the word.
还有其他一些限制!
From Googletrans 2.3.0 documentation
Note on library usage
- Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times. (so please use this library if you don’t care about stability.)
- If you want to use a stable API, I highly recommend you to use Google’s official translate API.
- If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.
该错误具有误导性,它的发生是因为您因多次请求而被 google 拒绝。我可以通过两种不同的方式解决这个问题:
- 使用 VPN 并更改您的 IP。它会再次起作用。
- 转到 google 并搜索一些内容。您将收到一条消息和一个需要解决的 reCAPTCHA。执行此操作后,它将再次工作一段时间。
我正在尝试使用 'googletrans' 模块将 100,000 个英语单词翻译成韩语。但经过一些迭代后,它引发了
'JSONDecodeError: Expecting value: line 1 column 1 (char 0)'.
我想弄明白,但网络上的解决方案对我不起作用。
我试过的是
- 每次迭代都重新初始化 Translator(),
- 每次迭代的睡眠时间 (.4)。
最奇怪的是它是随机发生的。有时它会在几百次迭代后出现,有时会在几次迭代后出现。
我查了这个模块的手册,我能找到的唯一限制是它只限制了单词的长度。但是,我要翻译的所有单词都只是字面上的单词,而不是短语或句子。
key_list = list(senticnet.keys())
for key in key_list:
translator = Translator()
time.sleep(.4)
print(translator.translate(key, dest='ko'))
上面代码中,senticnet是一个字典变量。看起来像
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)
Question: I checked manual of this module and the only limit I could find was that it only limits the length of the word.
还有其他一些限制!
From Googletrans 2.3.0 documentation
Note on library usage
- Due to limitations of the web version of google translate, this API does not guarantee that the library would work properly at all times. (so please use this library if you don’t care about stability.)
- If you want to use a stable API, I highly recommend you to use Google’s official translate API.
- If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address.
该错误具有误导性,它的发生是因为您因多次请求而被 google 拒绝。我可以通过两种不同的方式解决这个问题:
- 使用 VPN 并更改您的 IP。它会再次起作用。
- 转到 google 并搜索一些内容。您将收到一条消息和一个需要解决的 reCAPTCHA。执行此操作后,它将再次工作一段时间。