Get a Chinese explanation of a word from Youdao Dictionary Web page.
Import reimport urllibword=raw_input (' Input a word\n ') url= ' http://dict.youdao.com/search?q=%s '%word content= Urllib.urlopen (URL) pattern=re.compile ("", Re. Dotall) Result=pattern.search (Content.read ()). Group () Pattern2=re.compile ('
.*?') for I in Pattern2.findall (result): print I.strip ('
'). Strip (''). Decode (' Utf-8 ')
And then we'll 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: Beginmanimport Osimport sysimport urllibimport urllib2reload (SYS) sys.setdefaultencoding ("Utf-8") import Simplejson as Jsonimport Platformimport Datetimeapi_key = ' ****** ' keyform = ' ****** ' def gettranslate (txt): url = ' Http://fanyi.youdao.com/openap I.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 result def Sjson (json_data): query = json_data.get (' query ', ') # queried text translation = Json_data.get (' translation ', ') # translated basic = Json_data. Get (' basic ', ') # basic list sequence = Json_data.get (' web ', []) # phrase list Phonetic,explains_txt,seq_txt,log_word _explains = ', ', ', ', ' # more meanings if basic:phonetic = basic.get (' phonetic ', ') #Phonetic explains = Basic.get (' explains ', []) # More explanations list for obj in Explains:explains_txt + = obj+ ' \ n ' log_wor D_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 + = Explains_txt Print_format + = '-' *20+ ' \ n ' +seq_txt Print_format + = ' * ' *40+ ' \ n ' Print print_format choices = raw_input (U ' is writing the word book, reply (y/n): ') if choices in [' Y ', ' y ']: filepath = R '/h Ome/beginman/pyword/%s.xml '%datetime.date.today () if (Platform.system ()). lower () = = ' windows ': filepath = R ' E:\py Word\%s.xml '%datetime.date.today () fp = open (filepath, ' A + ') file = Fp.readlines () if not file:fp.write ('
\ n ') fp.write (U ""
"
\ n
%s
%s
[%s]
\ n \ n %s
\ n
1\
n \ n "" "% (Query,log_word_explains,phonetic,datetime.date.today ())) Fp.close () Print U ' write succeeded. ' def main (): While true:txt = raw_input (U ' Enter the text to query: \ n ') if Txt:sjson (gettranslate (TXT)) if __name__ = = ' __mai N__ ': Main ()
The above is all the content of this article, I hope you can enjoy