我正在尝试用以下示例代码翻译tweet列表:
from google_trans_new import google_translator
translator = google_translator()
translate_text = translator.translate('สวัสดีจีน', lang_src='th',lang_tgt='en')
print(translate_text)
当我运行代码时,我一直遇到以下长错误:
HTTPError Traceback (most recent call last)
~anaconda3libsite-packagesgoogle_trans_newgoogle_trans_new.py in translate(self, text, lang_tgt, lang_src, pronounce)
188 raise e
--> 189 r.raise_for_status()
190 except requests.exceptions.ConnectTimeout as e:
~anaconda3libsite-packagesrequestsmodels.py in raise_for_status(self)
940 if http_error_msg:
--> 941 raise HTTPError(http_error_msg, response=self)
942
HTTPError: 429 Client Error: Too Many Requests for url: https://www.google.com/sorry/index?continue=https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute&q=EgRrvwCgGLHwuIAGIhkA8aeDS9RXYOujcLlE7r1EY3pCFB3PU57xMgFy
During handling of the above exception, another exception occurred:
google_new_transError Traceback (most recent call last)
<ipython-input-1-e0a80cf9e6cc> in <module>
1 from google_trans_new import google_translator
2 translator = google_translator()
----> 3 translate_text = translator.translate('สวัสดีจีน', lang_src='th',lang_tgt='en')
4 print(translate_text)
5 #output: Hello china
~anaconda3libsite-packagesgoogle_trans_newgoogle_trans_new.py in translate(self, text, lang_tgt, lang_src, pronounce)
192 except requests.exceptions.HTTPError as e:
193 # Request successful, bad response
--> 194 raise google_new_transError(tts=self, response=r)
195 except requests.exceptions.RequestException as e:
196 # Request failed
google_new_transError: 429 (Too Many Requests) from TTS API. Probable cause: Unknown
这是因为我太频繁地使用翻译吗?什么时候重启,我才能继续工作?
google_trans_new是一个非官方的库,使用translate.google.com的web API,也与谷歌无关。
它正在抓取翻译网页,这就是为什么你最终收到一个429错误消息,这是HTTP 429 Too Many Request状态码。
这些非官方库是不稳定的,最终会被封锁。
要得到一个稳定的应用程序,你应该使用调用云翻译API的官方库。
Cloud Translation API有配额,但是它会使用指数回退来处理这些重试错误。