py-googletrans: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I am trying to translate a list of tokens from chinese to english. I get the following error after a few attempts of translation. Is this because of rate limits?
File "/home/sharath/.local/lib/python3.5/site-packages/googletrans/client.py", line 127, in translate
translated = self.translate(item, dest=dest, src=src)
File "/home/sharath/.local/lib/python3.5/site-packages/googletrans/client.py", line 132, in translate
data = self._translate(text, dest, src)
File "/home/sharath/.local/lib/python3.5/site-packages/googletrans/client.py", line 63, in _translate
data = utils.format_json(r.text)
File "/home/sharath/.local/lib/python3.5/site-packages/googletrans/utils.py", line 62, in format_json
converted = legacy_format_json(original)
File "/home/sharath/.local/lib/python3.5/site-packages/googletrans/utils.py", line 54, in legacy_format_json
converted = json.loads(text)
File "/data/anaconda2/envs/dlatk/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/data/anaconda2/envs/dlatk/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/data/anaconda2/envs/dlatk/lib/python3.5/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 35
- Comments: 33 (2 by maintainers)
Same error here. Problem now is that any attempt for translating is showing the same error
translator.translate("Hola", src = "es", dest = "en").textThrows:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "../py-googletrans/googletrans/client.py", line 172, in translate data = self._translate(text, dest, src) File "../py-googletrans/googletrans/client.py", line 81, in _translate data = utils.format_json(r.text) File "../py-googletrans/googletrans/utils.py", line 62, in format_json converted = legacy_format_json(original) File "../py-googletrans/googletrans/utils.py", line 54, in legacy_format_json converted = json.loads(text) File "/usr/lib/python3.5/json/__init__.py", line 319, in loads return _default_decoder.decode(s) File "/usr/lib/python3.5/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)@edmangog No, Ideas. But I am working a Script that solves this problem as a whole, give me the time of a week k, guys. The script works for 5K Words at one time and no limitation. No Json Decode Errors Ok Hold tight Guys. It is coming. Now, I am just testing it. And adding Languages
Same here, randomly stopped after going through about half my list. What’s weird is I broke it down into two loops and it also stopped after about half of that loop as well (from ~2500 entries to ~1200 entries). Seems like a rate limit as I added sleep(.4) to every loop of my script and it finally worked.
Everyone Listen! Carefully,
The Problem
Google restricts use of this after certain requests form your IP. So, that is why our IP is black listed and it gives the JSON DECODE ERROR!
Solution
Change your IP using a Free VPN while you are running your code. i.e. select a German IP, when it is blocked goto an American IP.
How to do that in Code
Conclusions
Just change ur IP’s lads! That is it. Do it Manually if you like. I use Psiphon3 VPN it is free. By using the above approach your code will be stable and it will work fine.
My Testing
I translated over 600K Words without an error using this approach. Linux/Ubuntu
On MacOS
See https://pypi.org/project/PyMultitor/
On Windows
Stick to the VPN Method. Or use services like torghost on Windows. But the Tor Approach is better
Other Problems
You can also get errors when there is an emoji in your text or a square box.
Solution
Filter your text to the standards that Google Translate can process and that will do the thing.
I think I have figured out the emoji issue. here is the emoji website: https://pypi.org/project/emoji/ install: pip install emoji
just need to demoji before translate, here is the sample str = ‘아니, 여기 왜 이르케 좋아앙?🙀♡’ translator = Translator() str_demoji = emoji.demojize(str) tran = translator.translate(str_demoji,src=‘ko’, dest=‘en’).text str_emoji = emoji.emojize(tran.replace(': ','😂.capitalize())
Hey Guys,
I’ve faced the same problem, and I think, that it’s caused by google’s restrictions. My temporary fix is to find and use proxy, which works.
hello, i found when i translate an Chinese sentences to English. The error happened, i had read the issues that the length limit and the emoji influence. Obviously, Not these two reasons. Coding is here:
And the error information:
I think I might have an idea on what’s causing this. I noticed that some emojis do work, but not the ones with code points within a special range (e.g. like the ones above 1F000). I think this is because the encoding Python uses to count characters, as opposed to JS which counts in UTF-16, causing the token generator to create the wrong token (the tokens didn’t match when I tested it with the web version of Google Translate). Check out this issue from the PHP version of the library for more info.
Did a workaround. Check this PR: #51
There should be some way of gracefully showing what the actual cause is
I also have this problem. It seems that it’s due to Google usage limits; however, sleeping or using a proxy hasn’t given any results so far.
Any idea?
I have this error too, and I found emoji in translating text cause this error.
There is Quite a simple Solution to this Reason Google thinks you are a bot and is Banning your IP Solution Use a VPN like Psiphon I created a try-except in translation, if there is an exception I just turn on VPN and it works fine. If it even blocks you on the VPN no Problem Just Change your Country, You can also automate this in Linux with Torghost. By Creating another .py file In that .py file os.system(“torghost -r”) Basically requests new exit Node and You are ready to go. You could use other services in Windows but in Linux use this one. Enjoy Cause we ain’t not paying google 20 bucks for google translate api Happy Coding