Python (googletrans) - AttributeError: 'NoneType' 对象没有属性 'group'

时间:2021-01-29 16:09:41

标签: python

尝试使用 googletrans 检测 Python 中的语言代码。但它会产生错误(请参阅错误块以获取信息)。

需要相同的解决方案

代码:

import googletrans
from googletrans import Translator
translator = Translator()
result = translator.translate('Mitä sinä teet')
print(result.src)
print(result.dest)
print(result.origin)
print(result.text)
print(result.pronunciation)

错误:


C:\ProgramData\Anaconda3\lib\site-packages\googletrans\gtoken.py in _update(self)
     60 
     61         # this will be the same as python code after stripping out a reserved word 'var'
---> 62         code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
     63         # unescape special ascii characters such like a \x3d(=)
     64         code = code.encode().decode('unicode-escape')

AttributeError: 'NoneType' object has no attribute 'group'

2 个答案:

答案 0 :(得分:0)

这是 googletrans 库的 github 页面中已知的已报告问题。查看那里的修复状态和潜在的变通方法:https://github.com/ssut/py-googletrans/issues

根据最新评论,安装不同版本 googletrans==4.0.0-rc1 似乎可以工作,但需要注意:

<块引用>

fire17 16 天前评论
正常pip安装失败但卸载并重新安装
googletrans==4.0.0-rc1
然后工作:)
尽管对象很奇怪,我可以访问翻译后的文本,例如

a = translator.translate("hi")
translated_text = a.__dict__()["text"]

答案 1 :(得分:0)

它在重新安装 googletrans 后工作

pip install googletrans==4.0.0-rc1

相关问题