python爬虫项目 翻译器
程序员文章站
2022-05-29 21:06:55
...
先上代码
import requests
import json
import tkinter as tk
win = tk.Tk()
win.geometry("400x400")
win.title("词条查询器")
ccccc = tk.Label(win,text="请输入需要翻译的词条")
ccccc.pack()
bbb = tk.Entry(win,width=15)
bbb.pack()
def aaa():
shuru = str(bbb.get())
url = "https://fanyi.baidu.com/sug"
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.68"
}
data = {
"kw":shuru
}
response=requests.post(url=url,data=data,headers=headers)
dic_obj=response.json()
a = dic_obj.get("data")
for i in a:
j=i.get("k")
j1=i.get("v")
f=j,j1
ee.insert("end", f)
ee.insert("end","\n")
ee.insert("end", "\n")
button = tk.Button(win, text="确定", width=15, command=aaa)
button.pack()
ee =tk.Text(win)
ee.pack()
win.mainloop()
代码运行情况
测试 输入中文后 结果如下
测试 输入英语后 结果如下
如有问题 可在讨论区留言: