Use Python to get word translations from the Youdao Dictionary Web page _python

Source: Internet
Author: User
Tags cdata

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 &LT;WORD&GT;%S&Lt;/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

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.