python使用爬虫写一个自己的翻译器(带图像界面)

python使用爬虫写一个自己的翻译器(带图像界面)

  大家好,我叫亓官劼(qí guān jié ),在CSDN中记录学习的点滴历程,时光荏苒,未来可期,加油~博客地址为:亓官劼的博客,B站昵称为:亓官劼,地址为亓官劼的B站

本文原创为亓官劼,请大家支持原创,部分平台一直在盗取博主的文章!!!


本文为python使用爬虫写一个自己的翻译器(带图像界面),效果图为:
python使用爬虫写一个自己的翻译器(带图像界面)_第1张图片

代码为:

import tkinter
import random
import requests
import request
import urllib
from  urllib import request,parse
import time,json,random,hashlib
win = tkinter.Tk()

def pachong():
    try:
        url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
        data = {}

        u = 'fanyideskweb'
        d = content
        f = str(int(time.time() * 1000) + random.randint(1, 10))
        c = 'rY0D^0\'nM0}g5Mm1z%1G4'

        sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()

        data['i'] = content  # 需要翻译的内容
        data['from'] = 'AUTO'
        data['to'] = 'AUTO'
        data['smartresult'] = 'dict'
        data['client'] = 'fanyideskweb'
        data['salt'] = f
        data['sign'] = sign
        data['doctype'] = 'json'
        data['version'] = '2.1'
        data['keyfrom'] = 'fanyi.web'
        data['action'] = 'FY_BY_CL1CKBUTTON'
        data['typoResult'] = 'true'
        data = parse.urlencode(data).encode('utf-8')
        req = request.Request(url, data=data)
        response = request.urlopen(req)

        res = response.read().decode('utf-8')
        res = json.loads(res)
        res = res["translateResult"]
        return res[0][0]['tgt']
    except:
        print("cuowu")

def eBtn(event):

    global content
    content = entry_w.get()
    entry_r.config(entry_r,text = content)
    entry_r.delete(0,80)
    entry_r.insert(0,str(pachong()))
    # pachong()


if __name__ == "__main__":
    label_val_q = tkinter.Label(win,width = "80")
    label_val_q.pack(side = "top")
    label_val_q.config(label_val_q,text = "请输入要翻译的文本")
    entry_w = tkinter.Entry(win,width = "80")
    entry_w.pack(side = "top")
    btn = tkinter.Button(win, text="翻译")
    btn.pack(side = "top")
    btn.bind('',eBtn)

    label_val_q = tkinter.Label(win,width = "80")
    label_val_q.pack(side = "top")
    label_val_q.config(label_val_q,text = "翻译为:")
    entry_r = tkinter.Entry(win,width = "80")
    entry_r.pack(side = "top")
    entry_r.bind('',eBtn)
    win.mainloop()




  大家好,我叫亓官劼(qí guān jié ),在CSDN中记录学习的点滴历程,时光荏苒,未来可期,加油~博客地址为:亓官劼的博客,B站昵称为:亓官劼,地址为亓官劼的B站

本文原创为亓官劼,请大家支持原创,部分平台一直在盗取博主的文章!!!

你可能感兴趣的:(python)