Ker*_*rem 11 python google-translate
我在迭代中翻译数千个文本数据时遇到此错误:
Expecting value: line 1 column 1 (char 0)
Run Code Online (Sandbox Code Playgroud)
我翻译大量文本的代码:
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)
Run Code Online (Sandbox Code Playgroud)
翻译大约2-3k行后,我收到此错误.
Ker*_*rem 19
我有点想出了问题.我认为这是关于Google API的请求限制.
我通过在每次迭代时重新初始化翻译器API来解决这个问题:
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)
Run Code Online (Sandbox Code Playgroud)
小智 9
这是我绕过他们的 API 调用限制所必须做的……我使用 VPN,特别是 Nord-Vpn,所以要按照我的方式进行操作,您需要能够通过以下方式连接/断开 VPN终点站...
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
Run Code Online (Sandbox Code Playgroud)