谷歌翻译python接口

项目地址:  https://github.com/ssut/py-googletrans

安装: 

sudo pip install googletrans

使用:

#!/usr/bin/python
# coding: UTF-8
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
from googletrans import Translator
translator = Translator()
print translator.translate('co-founder', dest='zh-CN',src='en')

结果:

/usr/bin/python2.7 /home/dahu/PycharmProjects/SpiderLearning/pytorch_lianxi/gugeapi.py
Translated(src=en, dest=zh-cn, text=联合创始人, pronunciation=None)

Process finished with exit code 0

本来想直接构造查询单词的url地址,但是在爬取的时候获取不到那个值,里面有个tk值不知道.

 看了下源码又修改了一下:

#!/usr/bin/python
# coding: UTF-8
import sys
reload(sys)
sys.setdefaultencoding('UTF-8')
from googletrans import Translator
translator = Translator()
with open('tmp1','r') as f:
    for line in f:
        # print translator.translate('co-founder', dest='zh-CN',src='en')
        a=translator.translate(line, dest='zh-CN',src='en')
        print line.strip(),getattr(a,"text")
Segmentation 分割
Motivation 动机
evaluate 评估
Perplexity 困惑

Process finished with exit code 0

直接提取所翻译的字

 

posted @ 2017-08-31 10:39  dahu1  Views(1182)  Comments(0Edit  收藏  举报