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

荒凉一梦 提交于 2019-11-26 14:38:41

问题


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.


回答1:


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)



回答2:


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




回答3:


In my case, the error was caused by too many requests in a short time period and my IP address was temporarily blocked. I tried it the next day again and everything worked well.




回答4:


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



回答5:


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




回答6:


I have also faced this problem. In my case, it was due to translating text (in english) to english.

As a workaround I have used another package langdetect to route the non-english text to be translated using google translate.

some snippet from the code:

from langdetect import detect
lang = detect(title)
if lang == 'en':
    temp_dict['title'] = title
else:
    temp_dict['title'] = translator.translate(title, dest='en').text


来源:https://stackoverflow.com/questions/49497391/googletrans-api-error-expecting-value-line-1-column-1-char-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!