#仅供学习参考。
目标网站:网易翻译+pyqt5 = 在线翻译.exe
功能需求:中英互译,其他国家语言也行,如果能用键盘打得出来得话,这部分功能没怎么测试。
界面介绍:因为有时候网络有延迟,所有增加了个翻译消耗时间。
具体代码实现。往下看。
以下为效果图:
功能测试图。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QTextEdit, QTextBrowser, QHBoxLayout, QVBoxLayout,QPushButton
import hashlib
import time,random
import requests"""
完成时间:2021/03/02
目前实现了功能,代码未梳理
可能日后需要维护,加密方式可能有变
"""url3 = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers2 = {'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=1906235112.3537061; OUTFOX_SEARCH_USER_ID="1285038361@10.169.0.84"; _ga=GA1.2.2001922211.1610463059; JSESSIONID=aaaD5ZbxHH8EWdyu--EFx; ___rl__test__cookies=1614354065751','Referer': 'http://fanyi.youdao.com/','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
}class Demo(QWidget):def __init__(self):super(Demo,self).__init__()self.setWindowTitle('中英翻译')self.edit_label = QLabel('中文\英文输入:',self)self.browser_label = QLabel('翻译输出:',self)self.time_label = QLabel('翻译消耗时间:',self)self.show_time = QLabel(self)#self.show_time.resize(20,60)self.sent_button = QPushButton('翻 译',self)self.sent_button.setEnabled(False)self.clear_button = QPushButton('清 除',self)self.text_edit = QTextEdit(self)self.text_edit.setPlaceholderText("请输入中文或者英文。Please input Chinese or English.")self.text_browser = QTextBrowser(self)self.text_browser.setPlaceholderText("ver:1.0/pin/20210203")self.time_H_layout = QHBoxLayout()self.sent_H_button = QVBoxLayout()self.edit_v_layout = QVBoxLayout()self.browser_v_layout = QVBoxLayout()self.all_h_layout = QVBoxLayout()self.layout_init()self.text_edit_init()def layout_init(self):self.edit_v_layout.addWidget(self.edit_label)self.edit_v_layout.addWidget(self.text_edit)self.sent_H_button.addWidget(self.sent_button)self.sent_H_button.addWidget(self.clear_button)self.time_H_layout.addWidget(self.time_label)self.time_H_layout.addWidget(self.show_time)self.browser_v_layout.addWidget(self.browser_label)self.browser_v_layout.addWidget(self.text_browser)#self.browser_v_layout.addWidget(self.time_H_layout)self.all_h_layout.addLayout(self.time_H_layout)self.all_h_layout.addLayout(self.edit_v_layout)self.all_h_layout.addLayout(self.sent_H_button)self.all_h_layout.addLayout(self.browser_v_layout)self.setLayout(self.all_h_layout)def text_edit_init(self):#self.text_edit.textChanged.connect(self.show_text_func)self.clear_button.clicked.connect(self.clear_func)self.sent_button.clicked.connect(self.trans_func)self.text_edit.textChanged.connect(self.cheack_FALSE)def trans_func(self):input_str = self.text_edit.toPlainText()self.wangyi(input_str)def show_text_func(self):self.text_browser.setText(self.text_edit.toPlainText())def clear_func(self):self.text_edit.clear()self.text_browser.clear()#self.sent_button.setEnabled(True)def cheack_FALSE(self):if len(self.text_edit.toPlainText())==0:#print("通过长度判断")self.sent_button.setEnabled(False)else:self.sent_button.setEnabled(True)def md5(self,value):md5_obj = hashlib.md5()md5_obj.update(bytes(value, encoding='utf-8'))sign = md5_obj.hexdigest()return signdef wangyi(self,input_str):salt = ""+str(int(time.time() * 1000)) + str(random.randint(0, 9))#以下加密信息,可能具有时效性,估计过一段时间,网易会更新字符串。如果更新了,翻译结果会出现翻译不全,或者少一截之类的异常情况。如有发生请自行更新,或联系本账号sign = self.md5('fanyideskweb' + input_str + salt + "Tbh5E8=q6U3EXe+&L[4c@") #影响翻译字符串长度以及翻译效果 1、n%A-rKaT5fb[Gy?;N5@Tj 2、Tbh5E8=q6U3EXe+&L[4c@data2 = {'i': input_str,'client': 'fanyideskweb','salt': salt,'sign': sign,'keyfrom': 'fanyi.web'}#print(salt,sign)try:t1 = time.time()respone = requests.post(url=url3, headers=headers2, data=data2).json()tgt = respone['translateResult'][0]#如果 sign 数据有问题导致的异常。t2 = time.time()timee = t2-t1self.show_time.setText("%.3fs"%timee)self.text_browser.setText(tgt)self.sent_button.setEnabled(False)except:self.text_browser.setText("响应超时,请重试!")self.sent_button.setEnabled(False)#self.text_browser.setText()if __name__ == '__main__':app = QApplication(sys.argv)demo = Demo()demo.show()sys.exit(app.exec_())