It doesn’t matter if you’re 14 or 15 or 40 or 50, if you love it and you’re willing to keep doing it, it’s going to pay off.


This article was submitted by a “little” reader in the programming classroom (students in the mutual Learning group 1 should be familiar with the author’s name). As I watched him churn out new code and technical articles, I marveled at the fear of the younger generation.


This is a crawler basic analysis and operation of the development of the case, to share with you.


When I need to translate a word on my computer, I do this:

Open the browser => open Baidu Translate => enter the word => get the translation

Sometimes, though, WHEN I’m writing code on the command line, I don’t want to switch to the browser and wait for the page to load.

So I thought, what if I could make a command-line translation tool with python?

Just do it!

A bit of Internet research, a bit of manipulation, and I finally figured it out. Here’s the final result:


In this tool, I used Baidu, Youdao and (optionally) Google translation.

The development train of thought

The principles of Youdao Translation and Baidu translation are almost the same. Here we choose Baidu Translation to play:

First open baidu translation: http://fanyi.baidu.com


Next, right-click to check and select XHR (data request to server) in Network. Then we enter the word “save” in the input box. We can see that there are several suG tags in the box. Click to have a look:


As you can see, the suG request contains the data we want. Click on the Request URL, which is the URL we need to Request. In addition, we can see that the Request Method is POST, which is to submit the form:


Moving to the end, we see the submitted form parameters:


In the Form Data, we see that the Form is a dictionary: {‘kw’: ‘save’}, where we typed save ourselves, which is the word we’re looking for, so we’ll use requests. Post to simulate the request:

# Url and form
url= 'https://fanyi.baidu.com/sug'
Form_data = {'kw': word}
Copy the code

# post request website response = requests.post(url. data = Form_data.headers=headers)

We can try to print out what is returned and you will find that it is returned in JSON format, so we need to introduce the built-in JSON library to parse it:

content = json.loads(response.text)   Load into dictionary formCopy the code

[‘data’][0][‘v’] [‘data’][0][‘v’]

result = content['data'] [0] ['v']   # Get translation results
return resultCopy the code

This completes the submission form and implements baidu translation.

Some common crawler analysis and operations are used here, and Youdao translation is basically similar, which will not be described again. Specific operations can be included in the complete code given at the end of the article.

Let’s take a look at the unusual Google Translate, which is a bit complicated. Google Translate has an API, but it costs money… I don’t want to pay… Here’s a quick look at how it works:


Operating in a similar way to Baidu Translation, we see an extra request here, which contains the desired translation result:


Let’s go back to the Request URL and see that we can just copy the previous one. Q is the word we need to translate, but the TK parameter is a tricky one. The solution I used in my code is online, using JavaScript to calculate this parameter, following the function provided on the Google Translate page (I suspect he just took the front-end source code, not carefully compared).

To execute JavaScript code in Python, we can use the library Pyexecjs (note that the import name of the library is execjs). The source code for this section is in the Py4Js class.

In particular, since Google Translate is not available directly, I’ve made it optional and won’t call it by default.

Method of use

I have posted the project to pypi and you can install it using the PIP command as follows:

pip install quicktranslateCopy the code

It’s also very simple to use (adding -g increases Google Translate results) :

Trans-t something you have to translateCopy the code

Example:

$ trans -t hello
= = = = = = = = = = = = = = = = = = = = = = = = =Youdao translate result: hello baidu translate result:[M: n ǐ h o] how do you do; how are you; hello;
= = = = = = = = = = = = = = = = = = = = = = = = =

Copy the code

$trans-t Hello -g= = = = = = = = = = = = = = = = = = = = = = = = =Youdao translate result: hello baidu translate result:[M: n ǐ h o] how do you do; how are you; hello;Google Translate result: Hellothere = = = = = = = = = = = = = = = = = = = = = = = = =

Full code of this case: github.com/pynickle/am… PIP install quicktranslate command

Author: pynickle

——

Learn together and go far!

Welcome to Crossin’s programming classroom