preface

I wrote two articles yesterday:

Baidu translation crawler for Python

Python’s Youdao translation crawler

Throw in Google Translate, put them together in a Demo, and make a simple translation app. Let’s have a good time

The development tools

Python version: 3.6.4
Related modules:

Requests module;

Pyqt5 module;

Js2py module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Introduction of the principle

The principle of Baidu Translation and Youdao Translation is no longer elaborated, here is a brief talk about the crawling principle of Google Translation.

After a brief test, the translation results are returned by requesting the link below:

The request needs to add many parameters, but only:

sl,tl,tk,q

These four parameters are not fixed. Q represents the word to be translated, SL represents the source language of translation, which can be set to auto(automatic detection), and TL represents the target language of translation. For convenience, only Chinese-English translation (tl is en) and English-Chinese translation (TL is zh-cn) are supported. The logic is that if the word to be translated contains Chinese, tl is en, otherwise zh-CN.

The q value determines the TK value. The related JS code is in a JS file named desktop_module_main.js:

Then I got lazy and directly copied the modified JS code from the reference to generate the required TK values. Finally, the source code for Google Translate content crawling is shown as the figure below:

Then integrate Google, Youdao and Baidu Translate:

To help upgrade those of you who are learning Python, here is a rich learning package

Complete source code see the package diagram for related files.