One day when translating using the googletrans library in python
'NoneType' object has no attribute 'group
I got the error, and after that, the process often failed due to this.
And even with the exact same code, there are times when you get an error and times when you don't. Troublesome.
Looking at [issues of py-googletrans](https://github.com/ssut/py-googletrans/issues/234), it was said that an error occurred due to the Google side, so for now it is a cure. I can't seem to do it.
However, temporary measures can be taken, so I will introduce them by excerpting them from the comments on the issue.
# Countermeasures
Sometimes you get an error and sometimes you don't, so do it many times until you succeed! !! !! It is a method. It is a push.
```python
from googletrans import Translator
src = "The text you want to translate"
tr = Translator()
while True:
try:
text = tr.translate(src, dest="en").text
break
except Exception as e:
tr = Translator()
Simply put, if you get an error using while and try-except as described above, recreate the instance and try again. And if the execution is successful, break through while and finish safely.
Thank you for your hard work.
Recommended Posts