#!/usr/bin/env python#coding=utf-8 " python use Youdao Dictionary API to implement command line look up the word" "Import urllib2import jsonimport sys reload (SYS) sys.setdefaultencoding (' utf-8 ') key = ' 1096888977 ' Keyfrom = ' bloketest ' doctype = ' json ' u = ' http://fanyi.youdao.com/openapi.do ' class req: def __init__ (self): self.key = key self.keyfrom = keyfrom Self.doctype = doctype def dict (self, words ): url = '%s?keyfrom=%s&key=%s&type=data&doctype=%s&version=1.1&q=%s ' % (U, self.keyfrom, self.key, self.doctype, words) solution_json = urllib2.urlopen (URL). Read () solution_final = json.loads (Solution_json) try: solutions = solution_final[' Translation '] print "[ \033[1;32;10m answer \033[0m ]" for item in solutions: print ' ' + item except KeyError: print "no solution ..." #扩展释义 try: solutions = solution_final[' Basic ' [' explains ']    &Nbsp; print "[ \033[1;32;10m extension \033[0m ]" for item in solutions: print ' ' + item except: pass #网络释义 try: solutions = solution_final[' web '] print "[ \033[1;32;10m network \033 [0m ] " for item in solutions[0].iteritems (): print ' ' + item[1][0] print except KeyError, e: print passif __name__ == ' __main__ ': while 1: try: words = raw_input (U ' \033[1;36;10m asked:\033[0m '). Strip () except EOFError, e: print "\nexit !" break except keyboardinterrupt, e: print "\nexit !" break if len (words) == 0: continue if words == ' Quit ': Break req (). Dict (words)
Ubuntu under the python script call Youdao dictionary API implementation command line look up the word