Python简单爬虫有道翻译,有GUI窗口,用到request库和tkinter

mac2024-10-06  25

from tkinter import * import requests root=Tk() Label(root,text="待翻译").grid(row=0,column=0) Label(root,text="已翻译").grid(row=1,column=0) v1=StringVar() v2=StringVar() e1=Entry(root,textvariable=v1,background='red') e2=Entry(root,textvariable=v2) e1.grid(row=0,column=1,padx=10,pady=5) e2.grid(row=1,column=1,padx=10,pady=5) def show(): content = e1.get() url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' kv = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36' } data = {'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': '15720936967797', 'sign': '632f606ac7bfb3eb82c6d16642f1780e', 'ts': '1572093696779', 'bv': 'f4d62a2579ebb44874d7ef93ba47e822', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_CLICKBUTTION'} data['i'] = content r = requests.get(url, data, headers=kv, timeout=30).json() print("翻译结果为:") print(r['translateResult'][0][0]['tgt']) e2.delete(0,END)#删除e2中原有的内容 e2.insert(0,r['translateResult'][0][0]['tgt']) Button(root,text="翻译",width=10,command=show)\ .grid(row=3,column=0,sticky=W,padx=10,pady=5) Button(root,text="退出",width=10,command=root.quit)\ .grid(row=3,column=1,sticky=W,padx=10,pady=5) mainloop()

 

最新回复(0)