您好,登錄后才能下訂單哦!
這篇文章主要介紹如何使用python實(shí)現(xiàn)一款屬于自己的翻譯詞典軟件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
1.先寫一個(gè)登錄的py文件,用python的tkinter庫(kù)
2.再寫一個(gè)py文件用于爬取有道翻譯輸出欄的內(nèi)容
3.再利用python的tkinter庫(kù),完成軟件運(yùn)行的窗口
4.將窗口的返回值與爬取有道翻譯的結(jié)果接口對(duì)一下
5.第二個(gè)py文件里import第一個(gè)py文件,兩個(gè)文件相關(guān)聯(lián)
請(qǐng)求方式為post,要注意from data里的值,這里可以在網(wǎng)頁(yè)上再輸一個(gè)想要翻譯的內(nèi)容,觀察from data里的值的變化,可以確定'salt'、 ‘sign'、 'lts'這三個(gè)值變化
進(jìn)入網(wǎng)頁(yè)源代碼搜索.js,找到對(duì)應(yīng)的js文件,找到他們的加密規(guī)則,最后將獲得的response轉(zhuǎn)化為字典,提取結(jié)果
class YouDao(object): a.LoginPage() def __init__(self): pass def crawl(self, content): # 進(jìn)入網(wǎng)頁(yè)源代碼搜索.js 點(diǎn)擊進(jìn)入搜索看是否有'salt''sign''lts'這三個(gè) 有則證明找的文件正確 全部復(fù)制 網(wǎng)頁(yè)搜js格式化轉(zhuǎn)化 創(chuàng)建js文件 lts = int(time.time() * 1000) # 時(shí)間戳轉(zhuǎn)化為毫秒 時(shí)間戳轉(zhuǎn)化為時(shí)間 站長(zhǎng)工具 timestamp = lts + random.randint(0, 10) # sign: n.md5("fanyideskweb" + e + i + "Tbh6E8=q6U3EXe+&L[4c@") a = "fanyideskweb" e = content i = str(timestamp) d = "Tbh6E8=q6U3EXe+&L[4c@" sign = hashlib.md5((a+e+i+d).encode('utf-8')).hexdigest() data = { 'i': content, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': timestamp, 'sign': sign, 'lts': lts, 'bv': 'dd67d51c2bbb03cccdbcfa48735ba27f', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_CLICKBUTTION' } data = urllib.parse.urlencode(data).encode('utf-8') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36', 'Cookie': 'OUTFOX_SEARCH_USER_ID=406040753@221.204.120.171; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; JSESSIONID=abcJcdIfpYuE3eNgyi3Dx; OUTFOX_SEARCH_USER_ID_NCOO=902501357.1460881; user-from=http://www.youdao.com/; from-page=http://www.youdao.com/; _ntes_nnid=250706e8175b6796101a34821527eb62,1612611490655; DICT_SESS=v2|qKlfGGGmbVOAOfp40fQy0UWhfPuOMzWRlfk4Qz0LYERTu0flfn4Ul0l50He40fOW0TK6LlWRf6B0w46LYmh56F0JShMTLOfOm0; DICT_LOGIN=1||1612612510172; STUDY_SESS=EtwvT8KhyXqnLv8r0zdde8FcMOICmtZSIsltEiKZiAQq44wPVNN6PupszWYKIkBKfygQdvKlXU7p3aF+p0H6VcZLod3s2Bld6H/EWIphmRS92qG/3vVhSxHFAXq2yJp8QyH/R6RElNstKdVewVkZp+NyGWhzlamzU5dl6aBiyQ2Ybdo8MpdaPQB26wR6JPAU+P6MxCmnJEvne6pPMc9TTJJnThNrM7aj0X5LVpSBvjZ0h4M1drl4ZsmtkumIhrpyk1pBNevj8UEmS52Cj8DFo+yez89Xrbg4rxsvfSmuH21KlOh/Gwx6G1S/X4FQ7qd/Z2lDsk6Qgl21Md/1bCxa/orloi9qObM4N2yVCVhvkDdg5ILQezB8iskCpUa+ESZk; STUDY_INFO=UID_10AE81F6EF9DD9807BAC3FF3FD6407BA|4|1456638755|1612612364854; ___rl__test__cookies=1612663596564', 'Referer': 'http://fanyi.youdao.com/', } request = urllib.request.Request('http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule', method='POST', data=data, headers=headers) response = urllib.request.urlopen(request) result_str = response.read().decode('utf-8') result_dict = json.loads(result_str) result = result_dict["translateResult"][0][0]["tgt"] return result
尤其注意按鈕這個(gè)地方
self.button = Button(self.window, text=u'查詢', command=self.function)
command命令執(zhí)行function
class Application(object): def __init__(self): # 創(chuàng)建一個(gè)窗口 self.window = Tk() # 窗口標(biāo)題 self.window.title(u'武亮宇翻譯詞典') # 設(shè)置窗口大小位置 self.window.geometry("280x350+400+150") # 輸入框 self.entry = Entry(self.window) self.entry.place(x=10, y=10, width=200, height=25) # width=寬度, height=高度 # 查詢按鈕 self.button = Button(self.window, text=u'查詢', command=self.function) # command執(zhí)行命令的意思 執(zhí)行這個(gè)函數(shù) self.button.place(x=220, y=10, width=50, height=25) # 翻譯結(jié)果標(biāo)題 self.label = Label(self.window, text=u'翻譯結(jié)果:') self.label.place(x=10, y=45) # 翻譯框 self.text = Text(self.window, background='#ccc') # 設(shè)置背景顏色 self.text.place(x=10, y=75, width=260, height=265)
function這塊要把爬蟲返回的值和窗口的翻譯框做一下接口
def function(self): # 從輸入框中獲取用戶的值 content = self.entry.get() # 把值發(fā)送給有道服務(wù)器進(jìn)行翻譯 youdao = YouDao() result = youdao.crawl(content) # 把結(jié)果放在翻譯框里 self.text.delete(1.0, END) # 每次查詢先刪除一下 self.text.insert(END, result) # 插入文本最后的位置 導(dǎo)入END庫(kù)
import urllib.request # 發(fā)送網(wǎng)絡(luò)請(qǐng)求 import urllib.parse # 參數(shù)解析 import time import random import hashlib import json from tkinter import Tk, Entry, Button, Label, Text, END # Tk(創(chuàng)建窗口), Entry(輸入框), Button(按鈕), Label(標(biāo)簽), Text(文本框), # END import a class YouDao(object): a.LoginPage() def __init__(self): pass def crawl(self, content): # 進(jìn)入網(wǎng)頁(yè)源代碼搜索.js 點(diǎn)擊進(jìn)入搜索看是否有'salt''sign''lts'這三個(gè) 有則證明找的文件正確 全部復(fù)制 網(wǎng)頁(yè)搜js格式化轉(zhuǎn)化 創(chuàng)建js文件 lts = int(time.time() * 1000) # 時(shí)間戳轉(zhuǎn)化為毫秒 時(shí)間戳轉(zhuǎn)化為時(shí)間 站長(zhǎng)工具 timestamp = lts + random.randint(0, 10) # sign: n.md5("fanyideskweb" + e + i + "Tbh6E8=q6U3EXe+&L[4c@") a = "fanyideskweb" e = content i = str(timestamp) d = "Tbh6E8=q6U3EXe+&L[4c@" sign = hashlib.md5((a+e+i+d).encode('utf-8')).hexdigest() data = { 'i': content, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': timestamp, 'sign': sign, 'lts': lts, 'bv': 'dd67d51c2bbb03cccdbcfa48735ba27f', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_CLICKBUTTION' } data = urllib.parse.urlencode(data).encode('utf-8') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36', 'Cookie': 'OUTFOX_SEARCH_USER_ID=406040753@221.204.120.171; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; JSESSIONID=abcJcdIfpYuE3eNgyi3Dx; OUTFOX_SEARCH_USER_ID_NCOO=902501357.1460881; user-from=http://www.youdao.com/; from-page=http://www.youdao.com/; _ntes_nnid=250706e8175b6796101a34821527eb62,1612611490655; DICT_SESS=v2|qKlfGGGmbVOAOfp40fQy0UWhfPuOMzWRlfk4Qz0LYERTu0flfn4Ul0l50He40fOW0TK6LlWRf6B0w46LYmh56F0JShMTLOfOm0; DICT_LOGIN=1||1612612510172; STUDY_SESS=EtwvT8KhyXqnLv8r0zdde8FcMOICmtZSIsltEiKZiAQq44wPVNN6PupszWYKIkBKfygQdvKlXU7p3aF+p0H6VcZLod3s2Bld6H/EWIphmRS92qG/3vVhSxHFAXq2yJp8QyH/R6RElNstKdVewVkZp+NyGWhzlamzU5dl6aBiyQ2Ybdo8MpdaPQB26wR6JPAU+P6MxCmnJEvne6pPMc9TTJJnThNrM7aj0X5LVpSBvjZ0h4M1drl4ZsmtkumIhrpyk1pBNevj8UEmS52Cj8DFo+yez89Xrbg4rxsvfSmuH21KlOh/Gwx6G1S/X4FQ7qd/Z2lDsk6Qgl21Md/1bCxa/orloi9qObM4N2yVCVhvkDdg5ILQezB8iskCpUa+ESZk; STUDY_INFO=UID_10AE81F6EF9DD9807BAC3FF3FD6407BA|4|1456638755|1612612364854; ___rl__test__cookies=1612663596564', 'Referer': 'http://fanyi.youdao.com/', } request = urllib.request.Request('http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule', method='POST', data=data, headers=headers) response = urllib.request.urlopen(request) result_str = response.read().decode('utf-8') result_dict = json.loads(result_str) result = result_dict["translateResult"][0][0]["tgt"] return result class Application(object): def __init__(self): # 創(chuàng)建一個(gè)窗口 self.window = Tk() # 窗口標(biāo)題 self.window.title(u'武亮宇翻譯詞典') # 設(shè)置窗口大小位置 self.window.geometry("280x350+400+150") # 輸入框 self.entry = Entry(self.window) self.entry.place(x=10, y=10, width=200, height=25) # width=寬度, height=高度 # 查詢按鈕 self.button = Button(self.window, text=u'查詢', command=self.function) # command執(zhí)行命令的意思 執(zhí)行這個(gè)函數(shù) self.button.place(x=220, y=10, width=50, height=25) # 翻譯結(jié)果標(biāo)題 self.label = Label(self.window, text=u'翻譯結(jié)果:') self.label.place(x=10, y=45) # 翻譯框 self.text = Text(self.window, background='#ccc') # 設(shè)置背景顏色 self.text.place(x=10, y=75, width=260, height=265) def function(self): # 從輸入框中獲取用戶的值 content = self.entry.get() # 把值發(fā)送給有道服務(wù)器進(jìn)行翻譯 youdao = YouDao() result = youdao.crawl(content) # 把結(jié)果放在翻譯框里 self.text.delete(1.0, END) # 每次查詢先刪除一下 self.text.insert(END, result) # 插入文本最后的位置 導(dǎo)入END庫(kù) def run(self): self.window.mainloop() if __name__ == '__main__': app = Application() app.run()
# !/usr/bin/nev python # -*-coding:utf8-*- import tkinter import tkinter.messagebox class LoginPage(object): def __init__(self): #聲明兩個(gè)變量 self.win = tkinter.Tk() # 窗口 self.username = tkinter.StringVar() self.password = tkinter.StringVar() self.n=2 self.createForm() def login(self): if self.username.get()=='新星計(jì)劃' and self.password.get()=='新星計(jì)劃': print('登錄成功') tkinter.messagebox.showinfo(title='登錄信息',message='登錄成功') self.win.quit() elif self.n==0: print('你沒(méi)有權(quán)限進(jìn)入該系統(tǒng)') self.win.quit() else: print('登錄失敗') print('賬號(hào)或密碼錯(cuò)誤,你還有%d次機(jī)會(huì)'%self.n) tkinter.messagebox.showerror(title='登錄信息',message='登錄失敗') self.n-=1 def createForm(self): self.win.title('登錄界面') self.win.geometry("280x200+400+150") #創(chuàng)建標(biāo)簽 labelname = tkinter.Label(self.win,text='用戶名:',justify=tkinter.RIGHT,width = 100) labelname.place(x=35,y=50,width=80,height=20) #創(chuàng)建文本框 entryname = tkinter.Entry(self.win, width=150, textvariable=self.username) entryname.place(x=110, y=50, width=120, height=20) #創(chuàng)建密碼標(biāo)簽 labelpwd = tkinter.Label(self.win,text='密 碼:', justify=tkinter.RIGHT,width=80) labelpwd.place(x=35, y=80, width=80, height=20) #創(chuàng)建密碼的文本框 entrypwd = tkinter.Entry(self.win, width=150,textvariable=self.password) entrypwd.place(x=110, y=80, width=120, height=20) #創(chuàng)建button按鈕 buttonOk = tkinter.Button(self.win,text='登錄',command=self.login) buttonOk.place(x=95,y=130,width=50,height=20) #創(chuàng)建退出的按鈕 buttonQuit = tkinter.Button(self.win,text='退出',command=self.win.quit) buttonQuit.place(x=145,y=130,width=50,height=20) self.win.mainloop() if __name__ == '__main__': lg = LoginPage()
一共兩個(gè)py文件,趕快收藏啊,快去試試吧,能擁有自己的翻譯詞典?。?!
先看一下軟件效果,先登錄
各種語(yǔ)言都可以轉(zhuǎn)換
以上是“如何使用python實(shí)現(xiàn)一款屬于自己的翻譯詞典軟件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。