2020/7/2:pip找不到py_translator了,无法pip安装。且报错超时。
改用translate包。
pip install translate
>>> from translate import Translator
>>> translator=Translator(from_lang="chinese",to_lang="english")
>>> translation = translator.translate("床前明月光,疑是地上霜;举头望明月,低头思故乡")
>>> translation
'The moonlight in front of the bed is suspected of frost on the ground; looking up at the moon, looking down at the hometown'
2020/6/19:py_translator测试可用。
win10 + anaconda + python 3.7.5 + py_translator-2.1.9
原本打算使用 googletrans,但是一直报超时错误:httpcore._exceptions.ConnectTimeout: timed out
所以改用py_translator,直接pip安装。
from py_translator import Translator
# 不指定service_urls会超时
t = Translator(service_urls=['translate.google.cn'])
s = t.translate('今天',src='zh-cn',dest='en').text
print(s) # Nowadays
s = t.translate('她是一个美丽的女人',src='zh-cn',dest='fr').text
print(s) # Elle est une belle femme