Python使用googletrans报错的解决方法

yipeiwu_com6年前Python基础

问题

最近在工作中发现了一个问题,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重新好使:

  • 卸载已有的googletrans;
  • git克隆最新的代码;
  • 安装包。

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

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

参考

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

相关文章

python正则分组的应用

复制代码 代码如下:import retext='V101_renow.Android.2.2.Normal.1.Alpha.apk?IMSI=460029353813976&MOBIL...

python通过shutil实现快速文件复制的方法

本文实例讲述了python通过shutil实现快速文件复制的方法。分享给大家供大家参考。具体如下: python通过shutil实现快速文件拷贝,shutil使用起来非常方便,可以通过p...

python创建关联数组(字典)的方法

本文实例讲述了python创建关联数组(字典)的方法。分享给大家供大家参考。具体分析如下: 关联数组在python中叫字典,非常有用,下面是定义字典的两种方法 # Dictionar...

在Python中处理字符串之isdigit()方法的使用

 isdigit()方法检查字符串是否只包含数字(全由数字组成)。 语法 以下是isdigit()方法的语法: str.isdigit() 参数  &...

Python对两个有序列表进行合并和排序的例子

假设有2个有序列表l1、l2,如何效率比较高的将2个list合并并保持有序状态,这里默认排序是正序。 思路是比较简单的,无非是依次比较l1和l2头部第一个元素,将比较小的放在一个新的列表...