Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleTrans API Error - Expecting value: line 1 column 1 (char 0)

I am having this error when translating thousands of text data in an iteration:

Expecting value: line 1 column 1 (char 0)

My code for translating big amounts of text:

translatedList = []
for index, row in df.iterrows():
    newrow = copy.deepcopy(row)
    try:
        # translate the 'text' column
        translated = translator.translate(row['text'], dest='en')
        newrow['translated'] = translated.text
    except Exception as e:
        print(str(e))
        continue
    translatedList.append(newrow)

I receive this error after translating about 2-3k rows.

like image 221
Kerem Avatar asked Mar 26 '18 17:03

Kerem


5 Answers

I kind of figured out the problem. I think that this is about Google API's request limit.

I solved this by reinitializing the translator API on every iteration:

import copy
from googletrans import Translator

translatedList = []
for index, row in df.iterrows():
    # REINITIALIZE THE API
    translator = Translator()
    newrow = copy.deepcopy(row)
    try:
        # translate the 'text' column
        translated = translator.translate(row['text'], dest='en')
        newrow['translated'] = translated.text
    except Exception as e:
        print(str(e))
        continue
    translatedList.append(newrow)
like image 194
Kerem Avatar answered Nov 01 '22 09:11

Kerem


This is what I had to do to bypass their API call restriction... I use a VPN, specifically Nord-Vpn, so to do it the way I did you would need to be able to connect/disconnect from/to a VPN through the terminal...

    def translate_text(text, dest_language="en"):
        # Used to translate using the googletrans library
        import json
        translator = googletrans.Translator()
        try:
            translation = translator.translate(text=text, dest=dest_language)
        except json.decoder.JSONDecodeError:
            # api call restriction
            process = subprocess.Popen(["nordvpn", "d"], stdout=subprocess.PIPE)
            process.wait()
            process = subprocess.Popen(["nordvpn", "c", "canada"], stdout=subprocess.PIPE)
            process.wait()
            return Process_Data.translate_text(text=text, dest_language=dest_language)
        return translation
like image 9
Austin Marino Avatar answered Nov 01 '22 11:11

Austin Marino


Google may be blocking your IP, use a VPN and it should work.

like image 5
Marc Bataillou Avatar answered Nov 01 '22 11:11

Marc Bataillou


There could be 2 reasons for this:
1. IP address is temporarily blocked.
2. You have reached the character limit.

I faced the same issue and ended up using another package called translate and it works flawlessly. The syntax is pretty similar too. You can find it here or do pip install translate

like image 4
Shayan Avatar answered Nov 01 '22 11:11

Shayan


In my case, it's caused by emoji in strings. I removed them and everything works well.

like image 3
Jeff Avatar answered Nov 01 '22 10:11

Jeff