Linux under the end of the use of Youdao translation

Source: Internet
Author: User

Linux uses the terminal, sometimes need to find a word, you need to open a browser to find words through the web page, so efficiency will be very cumbersome, and will also reduce productivity.

When I first started looking at a Python document, I read a script written in Python that looked up the word under the terminal, but didn't get it.

Today originally wanted to solve the problem of my website display, but think of this do not feel what is wrong, a feeling of obsessive-compulsive disorder.

I wrote a script in Python, with a Youdao translation API, thanks to Youdao.

Use the method for, Python youdao.py word, word word for the word you want to query.

Paste the following code:

Import sysfrom urllib.request import urlopenfrom html.parser import htmlparserdef get_result (word): url = '/http/ fanyi.youdao.com/openapi.do?keyfrom=majunzhe&key=27448872&type=data&doctype=xml&version=1.1 &q= ' Query_url = URL + wordresult = Urlopen (Query_url). Read (). Decode (' Utf-8 ') return ResultClass Wordresultparser ( Htmlparser): bhandled = Falsebexplain = falsedata_meaning = ' Output = ' tags = (' phonetic ', ' ex ', ' key ') def parse_data (sel F, data): Beg_pos = Data.rfind (' [') + 1end_pos = Data.find ('] ', Beg_pos, Len (data)) if End_pos = = -1:end_pos = Len (data) #pri NT (Beg_pos) #print (end_pos) #print (data) self.data_meaning = Data[beg_pos:end_pos]def Handle_starttag (self, tag, attrs ): if ' basic ' = = tag:self.output + = ' Basic explanation: \ n ' elif ' web ' = = tag:self.output + = ' network explanation: \ n ' elif ' explain ' = = Tag:self.bExplain = Trueif tag in self.tags:self.bHandled = Truedef handle_endtag (self, tag): if ' explain ' = = tag:self.output + = ' \ n ' Self.bex Plain = Falsedef unknown_decl (self, data): if self.bhandled! = True:returnself.parse_data (data) endstr = ' \ n ' If Self.bExplain:endstr = ' self.output + = self.data_meaning + endstrself. bhandled = Falsedef print_results (): For word in sys.argv[1:]: #print ('----------------------------------------------- --------------') # get the query Resultword_result = Get_result (Word) #print (word_result) Word_parser = Wordresultparser ( ) word_parser.feed (word_result) print (word_parser.output[:-2]) #print ('------------------------------------------ -------------------') print_results ()

The general steps are this:

1 First understand the Youdao translation API usage, the specific steps can be viewed on the official website, but before using the API to apply for a key.

2 through the Python library to open the URL, and by parsing the returned data to get query results, I use the Python library Htmlparser to parse the data.

3 with Linux under a simple configuration, you can use this script directly in the same way as using other command lines.

This script I set the ability to query multiple words at once.

The following are the configuration steps under Linux:

1 first put the above Python code into a file named youdao.py, I put this file in the ~/documents folder

2 Create a script under the ~/folder, and the file is named Youdao

#!/bin/bashwhile [$#-ne 0]dopython documents/youdao.py $1shiftdone
Where the while statement is used to query multiple words at a time

3 When you have completed the above steps, you can./youdao xxx, such a query word.

But it's certainly inconvenient for us to use it this way. Then link this script to/usr/bin.

sudo ln-s ~/youdao/usr/bin/youdao

4 complete.

The following can enjoy the command line under the query word, did not add the function of query sentence, feel no use, is generally looking for a word, check the function of multiple words at once or just in case.

Ha ha

Personal website is: www.majunzhe.com

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.