nidhaloff / deep-translator

A flexible free and unlimited python tool to translate between different languages in a simple way using multiple translators.
https://deep-translator.readthedocs.io/en/latest/?badge=latest
Apache License 2.0
1.61k stars 186 forks source link

RequestError when using deep_translator.GoogleTranslator.translate #182

Closed rashaduph26 closed 1 year ago

rashaduph26 commented 1 year ago

Description

Hi @nidhaloff,

Thanks for deep_translator!

I have encountered an issue with requesting using translate() function. When I pass ~200 texts for translation, it translates until 60 of them, then it stops and returns the following error.

RequestError: Request exception can happen due to an api connection error. Please check your connection and try again

I tried having time.sleep(2) considering the limit of requests has been exceeded, but it still runs for 60 texts, then stops.

Could you or anyone help me to figure out what goes wrong so I get the RequestError?

What I Did

translated_text = GoogleTranslator(source = 'auto', target = 'en').translate(text)
rashaduph26 commented 1 year ago

More elaboration

for i in tqdm(range(len(text_df))): translated_df['translation'][i] = translator(text_df['translation_text'][i], text_df['language'][i]) time.sleep(1) # 2, 3 did not also worked.

Error log:

[69] raise TooManyRequests() [71] if response.status_code != 200: ---> [72] raise RequestError() [74] soup = BeautifulSoup(response.text, "html.parser") [76] element = soup.find(self._element_tag, self._element_query)

RequestError: Request exception can happen due to an api connection error. Please check your connection and try again

nidhaloff commented 1 year ago

@rashaduph26 Sorry for the late reply! Yes, this is a well known issue and not really related to deep-translator. The google server (or any other translation server actually) won't let you do many requests, as you might have guessed.

In the past, I implemented some workarounds for it (e.g. the wait for 2, 3 seconds between requests), but still It won't solve the issue completely. One of the solutions that might work for you is to use a proxy, you can take a look in the documentation. There are some example on using proxies. Proxies are nice because the server would have no idea that you are making the requests all the time. Notice that using proxies is definitely slower as well, so you need to check if it is something for you.

Personally, I did not try a stress test to check this out, but if you do, it would be great to provide feedback here.