[Python] Countermeasures for "AttributeError:'NoneType' object has no attribute'group'" of googletrans

Introduction

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

[Python] Countermeasures for "AttributeError:'NoneType' object has no attribute'group'" of googletrans
AttributeError:'NoneType' object has no attribute'loader'
[Python] Attribute Error:'list' object has no attribute'replace'
Python: Get a list of methods for an object
[python] Value of function object (?)
Prolog Object Orientation for Python Programmers
Blender has no object selection order
Introductory table of contents for python3
Record of Python introduction for newcomers
With xmpppy'_ssl._SSLSocket' object has no attribute'issuer'
[Python] Minutes of study meeting for beginners (7/15)
Summary of various for statements in Python
[Python3] Rewrite the code object of the function
Pandas of the beginner, by the beginner, for the beginner [Python]
Summary of useful techniques for Python Scrapy
A story made by a person who has no knowledge of Python or Json