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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值