Linux terminal User Guide

Source: Internet
Author: User

Linux terminal User Guide

If you use a terminal in linux, you need to open a browser to search for words on a webpage. This will be very complicated and reduce your work efficiency.

When I first read the python document, I read a script written in python that I used to query words in the terminal. It was not recorded at the time and was not found later.

Today, I was trying to solve the problem of my website display. But I thought it would be wrong if I didn't do it, and I felt like an obsessive-compulsive disorder ..

I wrote a script in python and used an api for youdao translation. Thank you.

The usage is python youdao. py word, which is the word you want to query.

Below is the 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 (self, data): beg_pos = data. rfind ('[') + 1end_pos = data. find (']', beg_pos, len (data) if end_pos =-1: end_p OS = len (data) # print (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 definition: \ n' elif 'web' = tag: self. output + = 'network definition: \ 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. bExplain = Falsedef unknow N_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 ('authorization') # 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 as follows:

1. First understand how to use the api. You can view the specific steps on the official website, but you need to apply for a key before using the api.

2. Open the url through the python library and parse the returned data to obtain the query result. I use the python library HTMLParser to parse the data.

3. After simple configuration in linux, you can directly use the script like using other command lines.

This script sets the ability to query multiple words at a time.

The configuration steps in linux are as follows:

1. First, save the python code above into a file named youdao. py. I will place this file in ~ Under the/Documents folder

2 ~ /Create a script in the folder named youdao

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

3. Grant it executable permissions

 

chmod u+x youdao

4. After completing the preceding steps, you can query words like./youdao xxx.

But it is inconvenient for us to use it like this. Link the script to/usr/bin.

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

5.

Now, you can query words under the command line without adding the query sentence function. Generally, You can query a word, the function of querying multiple words at a time is just in case.

Haha

Personal Website: www.majunzhe.com


Is there any software similar to youdao translation in linux?

Yes, the star translation King is the most famous.

Youdao dictionary for linux in windows

No. You need to redevelop it.
However, interstellar translation can be used across platforms. You can try it on both windows and linux platforms. Or you can use the online dictionary directly.
For your reference, I wish you success!

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.