2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 基于python的有道词典接口爬取

基于python的有道词典接口爬取

时间:2020-04-22 17:04:44

相关推荐

基于python的有道词典接口爬取

from urllib import requestimport urllibimport refrom tkinter import *mygui = Tk(className="趣味翻译")mygui.geometry('1000x500+200+200')#设置窗口大小及位置header = {'user-agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36'}url = '/translate?smartresult=dict&smartresult=rule'def hit_me():form_date = {'i': e.get(),'from': 'AUTO','to': 'AUTO','smartresult': 'dict','client': 'fanyideskweb','salt': '15609295378881','sign': '1df1a958a9786081377324bce340e0da','ts': '1560929537888','bv': '8d165ec21fcdbdde58f225cd72fd33e4','doctype': 'json','version': '2.1','keyfrom': 'fanyi.web','action': 'FY_BY_CLICKBUTTION'}data = urllib.parse.urlencode(form_date).encode(encoding = 'utf-8')req = request.Request(url,headers=header,data=data )reponse = request.urlopen(req).read().decode()print(reponse)patt = r'"tgt":"(.*?)"}]]}'result = re.findall(patt,reponse)print(result)h = StringVar()fy = Entry(mygui, textvariable=h, font=('Arial', 20))h.set(result[0])fy.place(x=1200, y=250, anchor=CENTER)while True:l1 = Label(mygui, text="请输入您想翻译的中文或英文:")l1.place(x=800, y=200, anchor=CENTER)e = StringVar()word = Entry(mygui, textvariable=e, font=('Arial', 20))e.set('input your text here')word.place(x=800, y=250, anchor=CENTER)start = Button(mygui, text="开始翻译", command=hit_me)start.place(x=800, y=300, anchor=CENTER)mainloop()

运行结果:

通过在运行界面输入要翻译的话语,在后台显示翻译结果!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。