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

GoogleTrans API Error – Expecting value: line 1 column 1 (char 0) is an error which happens when your ip is temporarily blocked or you have reached the character limit.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Explaining the Error : GoogleTrans API Error – Expecting value: line 1 column 1 (char 0)

First of all we need to understand why the error happens at all, the error happens when your ip is temporarily blocked or you have reached the character limit.

The error usually happens when your code is translating a lot of text.

The error message looks like this, please make sure your error matches the message to avoid confusion.

                                                                       #
Expecting value: line 1 column 1 (char 0)
                                                                       #

Bellow I make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : use the ‘translate’ package

The solution is simple, use another package. You can visit the link bellow

                                                                       #
https://pypi.org/project/translate/
                                                                       #

And download the package.

You can also install the package using pip, like in the example bellow.

                                                                       #
pip install translate
                                                                       #

This solution should be enough but if that is not the case please try the solution bellow.

Solution 2 : use a vpn

The second solution is handy for people who have a vpn, this works for most people with nord vpn

You need to be able to connect and disconnect to the vpn using the terminal.

The code bellow, will help you a lot.

                                                                       #
translator = googletrans.Translator()
try:
translation = translator.translate(text=text, dest=dest_language)
except json.decoder.JSONDecodeError:
process = ubprocess.Popen(["nordvpn", "d"], stdout=subprocess.PIPE) # api call restriction
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
                                                                       #

If the vpn does not help try the solution bellow.

Solution 3 : reinitialize the translator API

The third solution is to reinitializing the translator API on every iteration:

You can avoid the API’s request limit using that.

You can use the following code as a base to start from. the two snippets should be joined together by putting them one after the other in your code.

                                                                       #
import copy
from googletrans import Translator
translatedList = []
for index, row in df.iterrows():
    # REINITIALIZE THE API
    translator = Translator()
    newrow = copy.deepcopy(row)
    try:
                                                                       #

Add the following snippet to 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)
                                                                       #

The solutions above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up

The article is over guys, I hope my effort did not go to waste and I helped some of you solve this error, keep learning Python and keep coding, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/