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

先去有道官网申请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)