Google 翻译成 python

Google translate in python

使用来自 https://stackabuse.com/text-translation-with-google-translate-api-in-python/

的代码时
from googletrans import Translator

translator = Translator()

result = translator.translate("Mitä sinä teet")

print(result.src)
print(result.dest)
print(result.origin)
print(result.text)

我收到“AttributeError: 'NoneType' object has no attribute 'group'”,我想知道出了什么问题,错误似乎在 'result = tra...' 行,

感谢您的帮助

好的,我也遇到了同样的错误。我认为当前版本中存在问题。但新的 alpha 版本运行良好。

首先卸载当前版本:

pip uninstall googletrans

然后安装这个:

pip install googletrans==3.1.0a0

我认为问题出在库的安装上,因为代码对我来说工作正常。我建议您卸载并重新安装 googletrans 库。

with open(os.path.join(files_from_folder, filename), encoding='utf-8') as html:
            soup = BeautifulSoup('<pre>' + html.read() + '</pre>', 'html.parser')
            for title in soup.findAll('title'):
                recursively_translate(title)

查看这 2 个完整代码,可以使用 python 轻松翻译文件:

https://neculaifantanaru.com/en/python-code-text-google-translate-website-translation-beautifulsoup-library.html

https://neculaifantanaru.com/en/example-python-google-translate-any-text-html-file-version-2.html