Ubuntu under the python script call Youdao dictionary API implementation command line look up the word

Source: Internet
Author: User
Tags python script

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.