Python Google Translate API error

*爱你&永不变心* 提交于 2019-11-28 08:59:27

问题


I am very to new to python and trying to translate a bunch of keywords using google API. I have an excel file with 3000 keywords which are mix of english, spanish, german etc. Trying to translate everything to English.

However, every time I run my code, I get error at different values. Sometimes, my code give error at 810th keyword while sometime it gives error at 1038 keyword. And I am not even editing the layout of the file.

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

Below is my code:

from googletrans import Translator
import pandas
import math
import time
df = pandas.read_excel(r'Desktop/python_keywords.xlsx')
keywords = df['Keywords']
Translate = []
translator = Translator()

for i in range(0,len(keywords)):
    word = translator.translate(str(keywords[j])).text
    Translate.append(word)

回答1:


Normally this error is due to the character limit of 15K in Googletrans API.

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

Consider reducing the number of characters.




回答2:


Which line of your code gives you this error? Look at error trace
Let's start with your iterators: it is declared as i, but then you use j. Then check length of your request. It shouldn't be longer than 5k symbols according to JSONDecodeError using Google Translate API with Python3.
Anyway, it looks like api responds with empty json and you have to add at least try .. except to avoid this error, smth like this:

try:  
    word = translator.translate(str(keywords[j])).text  
except JSONDecodeError as err:  
    print(err)  # if you want to see when error happens
else:
    Translate.append(word)


来源:https://stackoverflow.com/questions/48957624/python-google-translate-api-error

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