04
2019
12

Python使用googletrans报错

问题

Python代码一直用着免费的Google翻译API插件googletrans,这两天突然就报错了:

Traceback (most recent call last):
  File "xxx.py", line 5, in <module>
    result = translator.translate("Result from google translator", dest="zh-CN")
  File "/usr/lib/python3.4/site-packages/googletrans/client.py", line 172, in translate
    data = self._translate(text, dest, src)
  File "/usr/lib/python3.4/site-packages/googletrans/client.py", line 75, in _translate
    token = self.token_acquirer.do(text)
  File "/usr/lib/python3.4/site-packages/googletrans/gtoken.py", line 180, in do
    self._update()
  File "/usr/lib/python3.4/site-packages/googletrans/gtoken.py", line 59, in _update
    code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'

单独写个测试:

from googletrans import Translator;

if __name__ == "__main__":
    translator = Translator()
    result = translator.translate("Result from google translator", dest="zh-CN")
    print(result.text)

    result = translator.translate("使用免费谷歌自动翻译API", dest="en")
    print(result.text)

曝出同样的错误。

解决办法

谷歌爸爸的翻译API接口进行了改动,让一票翻译的软件包不好使了。问题影响是全球性的,所以GayHub上很快就跟进讨论,并提交代码修复。代码在手,方案我有,三步操作让googletrans重新好使:

  1. 卸载已有的googletrans;

  2. git克隆最新的代码;

  3. 安装包。

打开终端,三个命令搞定问题:

pip uninstall googletrans
git clone https://github.com/BoseCorp/py-googletrans.git
cd ./py-googletrans && python setup.py install

参考

  1. https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group

原文链接:https://www.qiquanji.com/post/4914.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

gzh

微信扫码关注

更新实时通知

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。