【问题标题】:Why "Googletrans.Translator" suddenly stopped working? [duplicate]为什么“Googletrans.Translator”突然停止工作? [复制]
【发布时间】:2019-02-26 01:05:44
【问题描述】:

几天前我在 Googletrans 中使用 Translator 功能写了几行。但是我今天试图重新运行这些行,它弹出了一系列错误......我对此感到非常困惑。如果您遇到过类似的问题,请在下方评论。欢迎任何帮助!

from googletrans import Translator
translator = Translator()
trans1 = translator.translate('Hello', dest = 'es')

我得到的错误如下:

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'

我试图对此错误消息进行研究,但一无所获。最让我困惑的是,这个简单的代码在 3 天前运行良好。但是当我今天早上打开时,我弹出了错误。请帮忙。非常感谢!

【问题讨论】:

    标签: python python-3.x google-translate language-translation


    【解决方案1】:

    我从this 链接获得了以下解决方案。

    解决方案:

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

    它非常适合我!

    【讨论】:

      【解决方案2】:

      更新py-googletrans/googletrans/gtoken.py,如下所示:

      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
      

      【讨论】:

        【解决方案3】:

        Google 更改了创建令牌的方式。 在撰写本文时没有修复。 您必须等待 googletrans 更新。

        【讨论】:

        • 嗨,克里斯,谢谢您的回答。你知道通常需要多长时间才能修复吗?根据您过去的经验,也许......或者您知道任何替代包/功能吗?再次感谢!
        • 现在是 2021 年 6 月,这个 bug 还活着并且在踢!我还要等多久才能更新? :))
        猜你喜欢
        • 2020-03-22
        • 2023-02-08
        • 2021-06-22
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多