Use Python to obtain word translation from the youdao dictionary webpage.

Source: Internet
Author: User

Use Python to obtain word translation from the youdao dictionary webpage.

Obtain the Chinese explanation of a word from the youdao dictionary webpage.

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("

I will share with you a command line version

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Date: 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/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 result def Sjson (json_data): query = json_data.get ('query', '') # query text translation = json_data.get ('translation ','') # Translation basic = json_data.get ('basic ', '') # basic list sequence = json_data.get ('web', []) # Phrase list phonetic, explains_txt, seq_txt, log_word_explains = '','' # More definitions if basic: pho Netic = basic. get ('phonetic ', '') # phonetic symbols explains = basic. get ('explains', []) # More definitions for obj in explains: 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 + = exp Lains_txt print_format + = '-' * 20 + '\ n' + seq_txt print_format + =' * 40 + '\ n' print print_format choices = raw_input, 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 (filepath, 'a + ') file = fp. readlines () if not file: fp. write ('<wordb Ook> \ 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 "% (query, log_word_explains, phonetic, datetime. date. today () fp. close () print U' is written successfully. 'def main (): while True: txt = raw_input (U' enter the text to be queried: \ n') if txt: Sjson (GetTranslate (txt )) if _ name _ = '_ main _': main ()

The above is all the content in this article. I hope you will like it.

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.