py-googletrans: AttributeError: 'NoneType' object has no attribute 'group'

I’m using one of the basic code:

from googletrans import Translator

translator = Translator()

translation=translator.translate('안녕하세요.', dest='ja')

print(translation)

And I’m getting an attribute error : AttributeError: 'NoneType' object has no attribute 'group'

Please let me know if the module is still working properly.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 21
  • Comments: 23

Most upvoted comments

Solution:

$ pip3 uninstall googletrans
$ pip3 install googletrans==3.1.0a0

Solution:

$ pip3 uninstall googletrans
$ pip3 install googletrans==3.1.0a0

Worked for me, thank you 😃

The issue is due to a failure followed by an invalid execution path that is not handled properly … it seems.

Ref: jtoken.py

  # in jtoken file (orig)
  raw_tkk = self.RE_TKK.search(r.text)
  if raw_tkk:
    self.tkk = raw_tkk.group(1)
    return

    code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
  # in jtoken file (modified)
  raw_tkk = self.RE_TKK.search(r.text)
  if raw_tkk:
    self.tkk = raw_tkk.group(1)
    return

    # we already called search() so let's just use the result which is raw_tkk
    code = raw_tkk.group(1).replace('var ', '')

As it can be noticed from the above two, which logically are equal

# raw_tkk is the result of calling self.RE_TKK.search(r.text)

raw_tkk = self.RE_TKK.search(r.text)

# If raw_tkk is valid, it return from the function as follow 
 if raw_tkk:
    self.tkk = raw_tkk.group(1)
    return

# If it is not valid, it does run
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')

# which is equal to 
code = raw_tkk.group(1).replace('var ', '')

# But we know we are there due to an invalid `raw_tkk`, and one cannot call `.group(1)` on an a NoneType `raw_tkk`.

Furthermore, I dump the repose text r.text and I noticed it looked like an error page in html, so the api is probably failing. Hope the above can help with a fix in RC2.

Also related

Solution:

$ pip3 uninstall googletrans
$ pip3 install googletrans==3.1.0a0

Works for me 🥇

Solution:

$ pip3 uninstall googletrans
$ pip3 install googletrans==3.1.0a0

It resolved my issue. Thanks.

Solution:

$ pip3 uninstall googletrans
$ pip3 install googletrans==3.1.0a0

have not fixed yet

I don’t even know why it didn’t work for you, but it helped me

It also solved my issue. Thanks