自己写的第一个Python小程序,刚开始学Python,语法都很基础,很多都还没学,只适合小白看。。。。
1.首先要设计一个Windows窗口,
root = Tk() root.title('翻译器') root['width'] = 250; root['height'] = 130
其中的组件有
(1)两个标签
Label(root, text = '输入要翻译的内容:', width = 15).place(x = 1, y = 1) Label(root, text = '翻译的结果:', width = 18).place(x = 1, y = 20)
(2)两个单行文本框(用Text也行,就是布局比较麻烦)
Entry1 = Entry(root, width = 20, textvariable = s1) Entry1.place(x = 110, y = 1) Entry2 = Entry(root, width = 20, textvariable = s2, state = 'readonly')#设置为只读状态 Entry2.place(x = 110, y = 20)
(3)三个按钮
Button1 = Button(root, text = '翻译', width = 8) Button1.place(x = 30, y = 80) Button2 = Button(root, text = '清空', width = 8) Button2.place(x = 100, y = 80) Button3 = Button(root, text = '退出', width = 8, command = root.quit) Button3.place(x = 170, y = 80)
2.然后根据对按钮的不同进行不同的事件处理
(1)翻译按钮
先将输入的翻译内容用爬虫到有道翻译进行翻译,翻译再提取翻译后的内容显示在窗口中
en_str = Entry1.get() vText = translate_Word(en_str) s2.set(str(vText))
(2)清空按钮
s1.set("") s2.set("")
3.最重要的爬取有道翻译的信息
(1)目标网址:'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
(2)表单数据制作成字典形式,后序会进行转换
(3)隐藏爬虫信息,伪装自己的头部
head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0'
这样就可以成功用爬虫爬取有道翻译的数据,只需要写成一个函数,返回翻译的内容即可实现翻译的功能
最后附上完整代码

1 '''有道翻译小程序''' 2 3 from tkinter import * 4 import urllib.request 5 import urllib.parse 6 import json 7 8 def translate_Word(en_str):#对传进来的参数进行翻译 9 10 #获取后台url 11 url='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule' 12 13 head={} 14 #隐藏爬虫信息,伪装自己的头部,字典 15 head['User-Agent']='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0' 16 17 #后台数据,获取表单 18 data={} 19 data['i']=en_str 20 data['from']='AUTO' 21 data['to']='AUTO' 22 data['smartresult']='dict' 23 data['client']='fanyideskweb' 24 data['salt']='15590456742344' 25 data['sign']='238073a9cc158731f9feea2f63589c3f' 26 data['ts']='1559045674234' 27 data['bv']='e2a78ed30c66e16a857c5b6486a1d326' 28 data['doctype']='json' 29 data['version']='2.1' 30 data['keyfrom']='fanyi.web' 31 data['action']='FY_BY_CLICKBUTTION' 32 33 data = urllib.parse.urlencode(data).encode('utf-8')#转化为标准utf-8编码 34 35 req = urllib.request.Request(url, data, head)#生成Request对象 36 response = urllib.request.urlopen(req)#传递数据 37 html = response.read().decode('utf-8')#读取信息并进行解码->Unicode 38 target = json.loads(html)#字符串->字典 39 return (target['translateResult'][0][0]['tgt']) 40 41 42 def leftClick(event):#翻译事件 43 44 en_str = Entry1.get()#获取要翻译的内容 45 vText = translate_Word(en_str)#通过有道翻译得到的结果 46 #Entry2.config(Entry2, text = vText)#设置修改组件属性 47 #先清空再插入,组件设置为'readonly' 48 #insert()和delete()方法对该组件直接被忽略 49 s2.set(str(vText)) 50 #Entry2.insert(0,vText) 51 52 def leftClick2(event):#清空事件 53 s1.set("") 54 s2.set("") 55 56 if __name__ == "__main__":#运行自己模块时执行 57 58 root = Tk() 59 root.title('翻译器') 60 root['width'] = 250; root['height'] = 130 61 62 Label(root, text = '输入要翻译的内容:', width = 15).place(x = 1, y = 1) 63 64 s1 = StringVar() 65 s2 = StringVar() 66 67 Entry1 = Entry(root, width = 20, textvariable = s1) 68 Entry1.place(x = 110, y = 1) 69 70 Label(root, text = '翻译的结果:', width = 18).place(x = 1, y = 20) 71 72 73 Entry2 = Entry(root, width = 20, textvariable = s2, state = 'readonly') 74 Entry2.place(x = 110, y = 20) 75 76 Button1 = Button(root, text = '翻译', width = 8) 77 Button1.place(x = 30, y = 80) 78 79 Button2 = Button(root, text = '清空', width = 8) 80 Button2.place(x = 100, y = 80) 81 82 Button3 = Button(root, text = '退出', width = 8, command = root.quit) 83 Button3.place(x = 170, y = 80) 84 85 #给Button设置监听事件 86 Button1.bind("<Button-1>", leftClick) 87 Button2.bind("<Button-1>", leftClick2) 88 #Button3.bind("<Button-1>", root.quit)!!!!不知道为什么不行 89 90 root.mainloop()