Why “Googletrans.Translator” suddenly stopped working?

落爺英雄遲暮 提交于 2019-12-08 02:49:28
AfnanAhmad

I got the solution below from this link.

Solution:

pip install git+https://github.com/BoseCorp/py-googletrans.git --upgrade

It worked perfectly for me!

DIRECTLY COPYING FROM: googletrans stopped working with error 'NoneType' object has no attribute 'group'

Apparently it's a recent and widespread problem on Google's side. Quoting various Github discussions, it happens when Google sends you directly the raw token.

It's being discussed right now and there is already a pull request to fix it, so it should be resolved in the next few days.

For reference, see:

https://github.com/ssut/py-googletrans/issues/48 <-- exact same problem reported on the Github repo https://github.com/pndurette/gTTS/issues/60 <-- seemingly same problem on a text-to-speech library https://github.com/ssut/py-googletrans/pull/78 <-- pull request to fix the issue

To apply this patch (without waiting for the pull request to be accepted) simply install the library from the forked repo https://github.com/BoseCorp/py-googletrans.git (uninstall the official library first):

$ pip uninstall googletrans $ git clone https://github.com/BoseCorp/py-googletrans.git $ cd ./py-googletrans $ python setup.py install You can clone it anywhere on your system and install it globally or while inside a virtualenv.

Update py-googletrans/googletrans/gtoken.py like the following:

RE_TKK = re.compile(r'TKK=eval\(\'\(\(function\(\)\{(.+?)\}\)\(\)\)\'\);',
                    re.DOTALL)
RE_RAWTKK = re.compile(r'tkk:\'([^\']*)\'',re.DOTALL)

def __init__(self, tkk='0', session=None, host='translate.google.com'):
    self.session = session or requests.Session()
    self.tkk = tkk
    self.host = host if 'http' in host else 'https://' + host

def _update(self):
    """update tkk
    """
    # we don't need to update the base TKK value when it is still valid
    now = math.floor(int(time.time() * 1000) / 3600000.0)
    if self.tkk and int(self.tkk.split('.')[0]) == now:
        return

    r = self.session.get(self.host)

    rawtkk = self.RE_RAWTKK.search(r.text)
    if rawtkk:
        self.tkk = rawtkk.group(1)
        return

Google has changed the way the token is created. There is no fix at the time of writing. You have to wait for googletrans to be updated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!