问题
I wrote a few lines using Translator function in Googletrans a few days ago. But I was trying to re-run those lines today and it popped a series of errors... I'm really confused by this. If you have experienced similar issue, please comment below. Any help is welcome!!
from googletrans import Translator
translator = Translator()
trans1 = translator.translate('Hello', dest = 'es')
The error I got is following:
AttributeError Traceback (most recent call last)
<ipython-input-19-c0f9e5495a2f> in <module>()
----> 1 trans1 = translator.translate('Hello', dest = 'es')
~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py in translate(self, text, dest, src)
170
171 origin = text
--> 172 data = self._translate(text, dest, src)
173
174 # this code will be updated when the format is changed.
~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\client.py in _translate(self, text, dest, src)
73 text = text.decode('utf-8')
74
---> 75 token = self.token_acquirer.do(text)
76 params = utils.build_params(query=text, src=src, dest=dest,
77 token=token)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py in do(self, text)
178
179 def do(self, text):
--> 180 self._update()
181 tk = self.acquire(text)
182 return tk
~\AppData\Local\Continuum\anaconda3\lib\site-packages\googletrans\gtoken.py in _update(self)
57 r = self.session.get(self.host)
58 # this will be the same as python code after stripping out a reserved word 'var'
---> 59 code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
60 # unescape special ascii characters such like a \x3d(=)
61 if PY3: # pragma: no cover
AttributeError: 'NoneType' object has no attribute 'group'
I tried to do research on this error msg, but got nothing. What confused me the most is that this simple code worked perfectly fine 3 days ago. But when i open up this morning, i popped errors. Please help. Thank you so much!
回答1:
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!
回答2:
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.
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/52446811/why-googletrans-translator-suddenly-stopped-working