您的位置:首页 > 编程语言 > Python开发

Python使用Youdao API 的有道词典小程序

2014-02-15 16:56 232 查看
先去有道官网申请API key,很容易申请到的.

使用方法:输入要翻译的中文或者英语,打印翻译结果,输入quit,程序结束

#!/usr/bin/env python
#coding:utf-8
#By eathings

import urllib2
import re
import json

class Youdao:
def __init__(self):
self.url = 'http://fanyi.youdao.com/openapi.do'
self.key = 'API key' #有道API key
self.keyfrom = 'keyfrom' #有道keyfrom

def get_translation(self,words):
url = self.url + '?keyfrom=' + self.keyfrom + '&key='+self.key + '&type=data&doctype=json&version=1.1&q=' + words
result = urllib2.urlopen(url).read()
json_result = json.loads(result)
json_result = json_result["translation"]
for i in json_result:
print i

youdao = Youdao()
while True:
msg=raw_input()
if msg == 'quit':
break
youdao.get_translation(msg)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python 有道词典 api json