Get a Chinese explanation of a word from the Youdao Dictionary Web page.
Import re
import urllib
word=raw_input (' Input a word\n ')
url= ' http://dict.youdao.com/search?q=%s '%word
content=urllib.urlopen (URL)
pattern=re.compile ("
To share a command-line version of
#!/usr/bin/env python #-*-coding:utf-8-*-# @Date: 2014-04-03 21:12:16 # @Function: Youdao translation Command line version # @Author: Beginman Import OS import sys import urllib import urllib2 reload (SYS) sys.setdefaultencoding ("Utf-8") import Simplejson as JSON IM Port Platform Import datetime api_key = ' Hu Jintao ' keyform = ' Hu Jintao ' def gettranslate (txt): url = ' Http://fanyi.youda O.com/openapi.do ' data = {' Keyfrom ': keyform, ' key ': Api_key, ' type ': ' Data ', ' doctype ': ' JSON ', ' Version ': 1 .1, ' Q ': txt} data = urllib.urlencode (data) url = url+ '? ' +data req = urllib2. Request (URL) response = Urllib2.urlopen (req) result = Json.loads (Response.read ()) Return to result def Sjson (Json_da TA): query = json_data.get (' query ', ') # text translation = Json_data.get (' translation ', ') # Translate basic = JS On_data.get (' basic ', ') # basic list sequence = Json_data.get (' web ', []) # phrase list Phonetic,explains_txt,seq_tx
T,log_word_explains = ', ', ', ', ' # more explanation if basic: Phonetic = basic.get (' phonetic ', ') # phonetic explains = Basic.get (' explains ', []) # more explanation list for obj in EXPL
Ains:explains_txt + = obj+ ' \ n ' log_word_explains = obj+ ', ' # sentence parsing if sequence:for obj in sequence: Seq_txt + = obj[' key ']+ ' \ n ' values = ' for I in obj[' value ']: values = = i+ ', ' seq_txt = Values+ ' \ n ' print_format = ' * ' *40+ ' \ n ' print_format + = U ' Query object:%s [%s]\n '% (query,phonetic) Print_format = E Xplains_txt Print_format + = '-' *20+ ' \ n ' +seq_txt print_format = ' * ' *40+ ' \ n ' Print print_format choices = Raw_input
(U ' write word, reply (y/n): ') if choices in [' Y ', ' y ']: filepath = R '/home/beginman/pyword/%s.xml '%datetime.date.today () if (Platform.system ()). lower () = = ' windows ': filepath = R ' E:\pyword\%s.xml '%datetime.date.today () fp = open (fi Lepath, ' A + ') file = Fp.readlines () if not file:fp.write (' <wordbook>\n ') fp.write (U "" "<item >\n <WORD>%S≪/word>\n <trans><! [Cdata[%s]]></trans>\n <phonetic><! [Cdata[[%s]]]></phonetic>\n <tags>%s</tags>\n <progress>1</progress>\n </item
>\n\n "" "% (Query,log_word_explains,phonetic,datetime.date.today ())) fp.close () print U ' write succeeded. ' def main (): While true:txt = raw_input (U ' Please enter text to query: \ n ') if Txt:sjson (gettranslate (TXT)) if __name__ = = ' __main__ ': Main ()
The above is all the content of this article, I hope you can enjoy