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)

Most upvoted comments

Same error here. Problem now is that any attempt for translating is showing the same error

translator.translate("Hola", src = "es", dest = "en").text

Throws:

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

#/usr/bin/python3

try:
    # Your Translation Code here
except:
    # Command to change IP using torghost or any other CLI based VPN

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

#Start Torghost
os.system('torghost -s')
#Change IP when exception
os.system('torghost -x')

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.

from py_translator import Translator
proxy = {
        'http': 'http://username:password@1.1.1.1:1234',
        'https': 'http://username:password@1.1.1.1:1234',
}
s = Translator(proxies=proxy).translate(text='Hello my friend', dest='es').text
print(s) 

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:

from googletrans import Translator
text = "唐高宗又在李世𪟝等朝廷武勋的模棱两可下"
translate = Translator()
en = translate.translate(text=text, dest='en').text 
print(en)

And the error information:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-18-8ecffbfeebab> in <module>()
      2 text = "唐高宗又在李世𪟝等朝廷武勋的模棱两可下"
      3 translate = Translator()
----> 4 en = translate.translate(text=text, dest='en').text
      5 print(en)

~/anaconda3/lib/python3.6/site-packages/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.

~/anaconda3/lib/python3.6/site-packages/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 

~/anaconda3/lib/python3.6/site-packages/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

~/anaconda3/lib/python3.6/site-packages/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 

~/anaconda3/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

~/anaconda3/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):

~/anaconda3/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)

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