Home  >  Article  >  Backend Development  >  Python implements translation software

Python implements translation software

little bottle
little bottleforward
2019-04-28 15:31:298804browse

This article mainly talks about using Python to implement translation software. It is super practical. Hurry and collect the code and try it. I hope it will inspire you to learn Python!

I ate a lot of dog food from Brother Ping two days ago. He wrote a translation software for his girlfriend, and I really felt the romance of being a programmer. I did a similar demo when I was learning requests. I sent a post request to Baidu Translate to translate any phrase. I further optimized that code and added an interactive interface on Saturdays and Sundays. With today’s translation software.

Introduction to the program

##The picture above will give you a feel

Python implements translation software

The function of the program is very simple. You can choose any translator from three mainstream translators to translate words and sentences, and use the PyQt5 module to achieve human-computer interaction. Use the requests module to send requests and return the translation results to the user.

Specific implementation

Use Baidu translation as an example

Any Translate to view page information.

Python implements translation software

You can see from the picture that this is a post request, and the request header data is also clearly displayed in the picture.

Python implements translation software

We need to add the above data, where simple_means_flag is a fixed amount, query represents the word to be translated, based on this information we write a simple Test the code.

import requests
headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36"}
post_data={
'query': 'Ahab杂货铺',
'from': 'zh',
'to': 'en',
'sign': '413120.175857',
'token':'64d8ce70799b54833f56b43f9d6eb3b4'
}

post_url="https://fanyi.baidu.com/v2transapi"
r=requests.post(post_url,data=post_data,headers=headers)
print(r.content.decode())

After running, the following results are output:

Python implements translation software

The reason for the above error is sign and token What’s wrong with these two parameters, let’s talk about the token first. The token can be found directly in the source code of Baidu Translation’s homepage:

Python implements translation software

because the timestamp is not synchronized Therefore, the token obtained by directly requesting Baidu Translate's homepage cannot be used. We can only manually copy the token value currently displayed on the webpage and assign it to the token in the code.

The sign parameter is generated in the frontend based on the translated content. If the query content and sign in the sent request do not match, the received response is an error. The next thing to do is to crack the Baidu Translate sign. sign is generated from a js file. The figure below is the js file that generates sign.

Python implements translation software

将这个代码放在格式化工具中重新排版一下,找到 sign 执行函数的代码,再用 execjs,执行这段 js 代码,在计算过程中还需要 gtk 的值,这个值在翻译首页获取一下就可以。

js = js.replace(

上边的步骤完成以后我们就可以愉快的进行翻译了。

图形化界面用的是 pyQt5 这个模块,实现起来不难。

class Demo(QWidget):
def __init__(self, parent=None):
super().__init__()
elf.setWindowTitle('翻译软件-公众号: Ahab杂货铺')
self.Label1 = QLabel('原文')
self.Label2 = QLabel('译文')
self.LineEdit1 = QLineEdit()
self.LineEdit2 = QLineEdit()
self.translateButton1 = QPushButton()
self.translateButton2 = QPushButton()
self.translateButton3 = QPushButton()
self.translateButton1.setText('百度翻译')
self.translateButton2.setText('有道翻译')
self.translateButton3.setText('谷歌翻译')
self.grid = QGridLayout()
self.grid.setSpacing(12)
self.grid.addWidget(self.Label1, 1, 0)
self.grid.addWidget(self.LineEdit1, 1, 1)
self.grid.addWidget(self.Label2, 2, 0)
self.grid.addWidget(self.LineEdit2, 2, 1)
self.grid.addWidget(self.translateButton1, 1, 2)
self.grid.addWidget(self.translateButton2, 2, 2)
self.grid.addWidget(self.translateButton3, 3, 2)
self.setLayout(self.grid)
self.resize(400, 150)
self.translateButton1.clicked.connect(lambda : self.translate(api='baidu'))
self.translateButton2.clicked.connect(lambda : self.translate(api='youdao'))
self.translateButton3.clicked.connect(lambda : self.translate(api='google'))
self.bd_translate = baidu()
elf.yd_translate = youdao()
self.gg_translate = google()
def translate(self, api='baidu'):
word = self.LineEdit1.text()
if not word:
       return
if api == 'baidu':
      results = self.bd_translate.translate(word)
elif api == 'youdao':
      results = self.yd_translate.translate(word)
elif api == 'google':
       results = self.gg_translate.translate(word)
else:
      raise RuntimeError('Api should be  or  or ...')
for result in results:
      self.LineEdit2.setText(result)

 相关教程:Python视频教程

The above is the detailed content of Python implements translation software. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete