python使用百度翻译api和网页爬虫百度翻译网页实现翻译小软件

it2022-05-05  73

import json import hashlib import requests import time from tkinter import * from tkinter import ttk from aip.speech import AipSpeech import pygame import os import execjs import threading # init 百度翻译api api_url = "http://api.fanyi.baidu.com/api/trans/vip/translate" my_appid = '填写自己的' cyber = '填写自己的' # 网页百度翻译爬虫翻译使用 jscode = """ function n(r, o) { for (var t = 0; t < o.length - 2; t += 3) { var a = o.charAt(t + 2); a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a), a = "+" === o.charAt(t + 1) ? r >>> a : r << a, r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a } return r } function e(r) { var o = r.match(/[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]/g); if (null === o) { var t = r.length; t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10)) } else { for (var e = r.split(/[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++) "" !== e[C] && f.push.apply(f, a(e[C].split(""))), C !== h - 1 && f.push(o[C]); var g = f.length; g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join("")) } var u = void 0 , l = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107); u = null !== i ? i : (i = "320305.131321201" || "") || ""; for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) { var A = r.charCodeAt(v); 128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)), S[c++] = A >> 18 | 240, S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224, S[c++] = A >> 6 & 63 | 128), S[c++] = 63 & A | 128) } for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++) p += S[b], p = n(p, F); return p = n(p, D), p ^= s, 0 > p && (p = (2147483647 & p) + 2147483648), p %= 1e6, p.toString() + "." + (p ^ m) } var i = null;""" h = { 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36', 'cookie': 'BAIDUID=5245FA6F6AD7018E7B44E09CFBEB7AF8:FG=1; BIDUPSID=5245FA6F6AD7018E7B44E09CFBEB7AF8; PSTM=1563149871; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; to_lang_often=[{"value":"en","text":"%u82F1%u8BED"},{"value":"zh","text":"%u4E2D%u6587"}]; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; from_lang_often=[{"value":"zh","text":"%u4E2D%u6587"},{"value":"en","text":"%u82F1%u8BED"}]; locale=zh; delPer=0; PSINO=6; H_PS_PSSID=1449_21116_29522_29521_29237_28519_29098_28831_29220_29071; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1563190015,1563190826,1563241841,1563245326; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1563245326; yjs_js_security_passport=a9453a70877639b0901c8d476d9deff20a12df98_1563245326_js' } is_text_change = 1 recent_use = '.mp3' class translator(object): def __init__(self): self.toL = 'en' self.toV = 0 self.t_method = 0 # 翻译方法 self.root = Tk() self.root.geometry('300x400+1100+140') self.root.title('翻译') self.toLanguage = {'英文': 'en', '中文': 'zh', '日文': 'jp', '韩语': 'kor'} self.name = StringVar() # 用户输入 self.combobox = ttk.Combobox(self.root, textvariable=self.name) self.combobox["values"] = ("英文", "中文", "日文", "韩语") self.combobox["state"] = "readonly" self.combobox.bind("<<ComboboxSelected>>", self.selectLanguage) self.combobox.current(0) self.toVcombobox = {'普通女声': 0, '普通男生': 1, '情感合成-度逍遥': 3, '情感合成-度丫丫': 4} self.name2 = StringVar() # 用户输入 self.comboboxV = ttk.Combobox(self.root, textvariable=self.name2) self.comboboxV["values"] = ("普通女声", "普通男生", "情感合成-度逍遥", "情感合成-度丫丫") self.comboboxV["state"] = "readonly" self.comboboxV.bind("<<ComboboxSelected>>", self.selectV) self.comboboxV.current(0) self.translator_method = {'百度api翻译': 0, '百度网页爬虫翻译': 1} self.name3 = StringVar() # 用户输入 self.translator_method_combobox = ttk.Combobox(self.root, textvariable=self.name3) self.translator_method_combobox["values"] = ("百度api翻译", "百度网页爬虫翻译") self.translator_method_combobox["state"] = "readonly" self.translator_method_combobox.bind("<<ComboboxSelected>>", self.select_tranlator_method) self.translator_method_combobox.current(0) self.btn_translate = Button(self.root, text="点击翻译", command=self.translate) self.btn_reset = Button(self.root, text="清空输入", command=self.reset) self.text_input = Text(self.root, height=8, width=30) self.text_output = Text(self.root, height=10, width=30, state='disabled') self.btn_speak = Button(self.root, text="点击读", command=self.speak) self.text_input.pack(fill=X) self.btn_translate.pack(fill=X) self.btn_reset.pack(fill=X) self.combobox.pack() self.comboboxV.pack() self.translator_method_combobox.pack() self.btn_speak.pack() self.text_output.pack(fill=X) self.root.protocol("WM_DELETE_WINDOW", self.callbackClose) self.root.mainloop() def callbackClose(self, *args): tempFile = '.\\tempAuido' if os.path.exists(tempFile): ls = os.listdir(tempFile) for i in ls: c_path = os.path.join(tempFile, i) if not os.path.isdir(c_path): try: os.remove(c_path) except: sys.exit(0) sys.exit(0) def select_tranlator_method(self, *args): t_method_temp = self.translator_method_combobox.get() for i in self.translator_method: if t_method_temp == i: self.t_method = self.translator_method[i] break def speak(self): # 开启了读的线程 speakthread = speakThread(1, "Thread-1", self.text_output.get(1.0, END), self.toV) speakthread.start() speakthread.join() def selectV(self, *args): global is_text_change is_text_change = True toVtemp = self.comboboxV.get() for i in self.toVcombobox: if toVtemp == i: self.toV = self.toVcombobox[i] break def selectLanguage(self, *args): tolanguage = self.combobox.get() for i in self.toLanguage: if tolanguage == i: self.toL = self.toLanguage[i] break def reset(self): self.text_input.delete(1.0, END) def translate(self): global is_text_change is_text_change = True if self.t_method == 0: word = self.text_input.get(1.0, END) # init salt and final_sign salt = str(time.time())[:10] final_sign = str(my_appid) + word + salt + cyber final_sign = hashlib.md5(final_sign.encode("utf-8")).hexdigest() # 区别en,zh构造请求参数 my_url = api_url + '?appid=' + str( my_appid) + '&q=' + word + '&from=' + 'auto' + '&to=' + self.toL + '&salt=' + salt + '&sign=' + final_sign response = requests.get(my_url).content content = str(response, encoding="utf-8") json_reads = json.loads(content) self.text_output.config(state=NORMAL) self.text_output.delete(1.0, END) self.text_output.insert(0.0, json_reads['trans_result'][0]['dst']) self.text_output.config(state=DISABLED) elif self.t_method == 1: word = self.text_input.get(1.0, END) sign = execjs.compile(jscode).call("e", word) # 准备检测单词类型的url url1 = 'https://fanyi.baidu.com/langdetect' d = { 'query': word } response = requests.post(url1, data=d) lan = response.json()['lan'] # 准备翻译单词的url url2 = 'https://fanyi.baidu.com/v2transapi' d['from'] = lan d['to'] = self.toL d['transtype'] = 'translang' d['simple_means_flag'] = '3' d['sign'] = sign d['token'] = 'b1fc3ff1eacdfca59e16195f2a956f84' response = requests.post(url2, data=d, headers=h) res = response.json()["trans_result"]['data'][0]['dst'] self.text_output.config(state=NORMAL) self.text_output.delete(1.0, END) self.text_output.insert(0.0, res) self.text_output.config(state=DISABLED) else: pass class speakThread(threading.Thread): def __init__(self, threadID, name, text_output, toV): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.text = text_output self.client = AipSpeech('14580055', 'QH2DzPrdQtwyOZCbHGVTytXC', '6CcFZtF3Pzsz0xGKB7BS9RYNXqQYRKEH ') self.toV = toV def run(self): global is_text_change global recent_use print("-------------------------" + str(is_text_change)) tempFile = '.\\tempAuido' if not os.path.exists(tempFile): os.makedirs(tempFile) if is_text_change: is_text_change = not is_text_change result = self.client.synthesis(self.text, 'zh', 1, { 'vol': 5, 'per': self.toV, 'spd': 5 }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): # 存储每个播放的语音 MP3name = int(time.time() * 1000) # 毫秒级时间戳 mp3file = tempFile + '\\' + str(MP3name) + '.mp3' recent_use = mp3file with open(mp3file, 'wb') as f: f.write(result) pygame.mixer.init() pygame.mixer.music.load(mp3file) pygame.mixer.music.play() while pygame.mixer.music.get_busy(): continue pygame.mixer.music.stop() pygame.mixer.quit() else: pygame.mixer.init() pygame.mixer.music.load(recent_use) pygame.mixer.music.play() while pygame.mixer.music.get_busy(): continue pygame.mixer.music.stop() pygame.mixer.quit() if __name__ == '__main__': translator()

 


最新回复(0)