preface

I wrote two articles yesterday:

The Python crawler

Python youdao translation crawler

I added Google Translate, then integrated them into a Demo, and made a simple translation software. Let’s get started happily

The development tools

Python version: 3.6.4
Related modules:

Requests module;

Pyqt5 module;

Js2py module;

And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

Introduction of the principle

Baidu translation and youdao translation principle is no longer elaborated, here is a brief talk about Google translation crawling principle.

After a brief test, we found that the translation result was returned by the following link:

There are many parameters to add to this request, but only:

sl,tl,tk,q

These four parameters are not fixed. Where Q stands for the word to be translated, SL stands for the source language of translation, which can be set to AUTO (i.e. automatic detection), and TL stands for the target language of translation. For convenience, only Chinese-English translation (TL is EN at this time) and English-Chinese translation (TL is ZH-CN at this time) are supported here. The logic realized is that if the vocabulary to be translated contains Chinese, TL is EN, otherwise it is ZH-CN.

Finally, Tk, Tk is generated by a piece of JS code, similar to Baidu translation, Q value determines the value of Tk, the relevant JS code in a JS file named desktop_module_main.js:

Then I slacked off and directly copied the modified JS code from the references to generate the required Tk value. Finally, the source code of Google translation content crawling is shown in the figure below:

Then integrate Google, youdao and Baidu translation:

To help those who are learning Python, there is a rich learning kit here

We’re done

See Personal Profile for complete source code related files.