python爬虫 |
您所在的位置:网站首页 › set百度翻译 › python爬虫 |
实现的效果
一、翻译结果 我们先对目标网页进行踩点,我们先使用F12进行检查,打开network,然后随便输入一个词语,观看网页返回信息的变化。当我们在查看XHR里的内容时,发现了我们需要的信息。 但是很遗憾他报错了,说 i not defind。 我们接下来就是找到i,我们使用watch对i进行监听。 其他的语言翻译也如法炮制,我们只需要更改一些信息就可以办到,因为sign都是一样的 二、用户界面的实现这里我们使用了tkinter,他是python自带的,使用起来也很简单,在这里,我就上代码就完事了。 import tkinter as tk from PIL import Image, ImageTk from en2zh import result # english to chinese from zh2en import result2 # chinese to english from tkinter import ttk from zh2kor import result3 # chinese to kor import os import pyperclip def xFunc(event): global my_flage global var_choice my_choice = com.get() # 获取选中的值方法1 if my_choice == '英语-->>中文': my_flage = 'en2zh' var_choice.set(my_choice) elif my_choice == '中文-->>英语': my_flage = 'zh2en' var_choice.set(my_choice) elif my_choice == '中文-->>韩文': my_flage = 'zh2kor' var_choice.set(my_choice) def en2zh(): global flage try: if flage and my_flage == 'en2zh': trans = e.get() fruit = result(trans) var.set(fruit) elif flage and my_flage == 'zh2en': trans = e.get() fruit = result2(trans) var.set(fruit) elif flage and my_flage == 'zh2kor': trans = e.get() fruit = result3(trans) # pyperclip.copy(fruit) var.set(fruit) except NameError: print('Pleace choose the Language!!') def clear(): global text text = l.cget('textvariable') if text is not None: var.set('') else: pass def sound(): try: os.system(r'D:\Laf\爬虫\爬虫实战\baiduifanyi' + '/' + my_flage + '/' + 'result' + '.mp3') except NameError: print('Worning!!') def copy_(): pyperclip.copy(var.get()) print("Ok to copy to the local!") # 设置主题窗户 root = tk.Tk() root.geometry('800x500') root.configure(background='#f8fabd') root.title('翻译软件') root.resizable(False, True) var_choice = tk.StringVar() xVariable = tk.StringVar() # #创建变量,便于取值 com = ttk.Combobox(root, textvariable=xVariable) # #创建下拉菜单 com["value"] = ('英语-->>中文', '中文-->>英语', '中文-->>韩文') # #给下拉菜单设定值 com.bind("", xFunc) # 设置条形框,插入图片 img = Image.open("1.jpg") width = img.size[0] # 获取宽度 height = img.size[1] img = img.resize((int(width*0.25), int(height*0.25)), Image.ANTIALIAS) photo1 = ImageTk.PhotoImage(img) var = tk.StringVar() imgs = Image.open("sound.png") width = imgs.size[0] # 获取宽度 height = imgs.size[1] imgs = imgs.resize((int(width*1), int(height*1)), Image.ANTIALIAS) photo_sound = ImageTk.PhotoImage(imgs) l = tk.Label(root, textvariable=var, height=4, width=80, font=('Arial Bold', 20)) Lab = tk.Label(root, textvariable=l.cget('textvariable'), image=photo1) e = tk.Entry(root, show=None, width=100) flage = True choose_label = tk.Label(root, textvariable=var_choice, height=4, width=20, font=('Arial Bold', 10), bg='#d53f27') b = tk.Button(root, text='翻译', width=15, height=2, command=en2zh) b1 = tk.Button(root, text='清除', width=5, height=1, command=clear) b1.place(x=750, y=422) # 设置清除按钮 b2 = tk.Button(root, image=photo_sound, width=20, height=20, command=sound) b3 = tk.Button(root, text='复制', width=3, height=2, font=('Arial Bold', 10), command=copy_) b3.place(x=650, y=422) b2.place(x=700, y=422) # 设置清除按钮 com.pack() # #将下拉菜单绑定到窗体 Lab.pack() # 设置主界面 e.pack() # 翻译输入文本 l.pack() # 显示翻译结果的Label b.pack() # 翻译按钮 choose_label.place(x=0, y=0) root.mainloop()这是我的文件的大概结构。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |