• 有道翻译


    from tkinter import *
    import urllib.parse
    import urllib.request
    
    
    def youdao(words):
        # 构建url  
        url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    
        # 构建请求头  
        headers = {
            "User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:59.0) Gecko/20100101 Firefox/59.0'
        }
    
        # 构建请求体  
        format_data = {
            'i': words,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': '1526368137702',
            'sign': 'f0cd13ef1919531ec9a66516ceb261a5',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTIME',
            'typoResult': 'true'
        }
    
        # 进行url编码  
        format_data = urllib.parse.urlencode(format_data).encode("utf-8")
    
        # 获取request文件(传入了data参数,就是post请求)  
        request = urllib.request.Request(url, data=format_data, headers=headers)
    
        # 打开请求文件  
        response = urllib.request.urlopen(request)
    
        # 读取文件内容  
        content = response.read()
        content = eval(content)
        ret = content["translateResult"][0][0]['tgt']
    
        print(ret)
        return ret
    
    
    # 主程序  
    root = Tk()
    # 设置标题  
    root.title("呆瓜词典")
    # 设置主窗口大小  
    root.geometry("320x150")
    # 可变大小  
    root.resizable(width=True, height=True)
    
    # 第一排输入框 输入查询的内容
    # 左边是一个标签  
    l1 = Label(root, text='查询内容', bg="yellow", font=(12), height=1, width=8)
    l1.place(x=20, y=20)
    var1 = StringVar()
    input_text = Entry(root, textvariable=var1)
    input_text.place(x=100, y=20)
    
    # 第二排显示框 显示查询的结果  
    # 左边是一个标签  
    l2 = Label(root, text='查询结果', bg="yellow", font=(12), height=1, width=8)
    l2.place(x=20, y=60)
    var2 = StringVar()
    output_text = Entry(root, textvariable=var2)
    output_text.place(x=100, y=60)
    
    
    # 调用youdao函数,传进要翻译的内容
    def func():
        words = var1.get()
        if words:
            # print(words)  
            result = youdao(words)
            var2.set(result)
    
        # 添加一个按钮
    
    
    b = Button(root, text="查询", command=func)
    b.place(x=170, y=100)
    
    # 运行主程序  
    root.mainloop() 
  • 相关阅读:
    Java基础课程---将一个字符串反转,将字符串中指定部分进行反转,比如,"abcdefg", 反转为"abfedcg"
    在Centon64位中卸载安装时系统自带的--openjdk,并且安装自己需要的jdk
    Java基础课程---sleep()方法 和 wait()方法的异同(面试题)
    Java基础课程---权限修饰符
    Java基础课程---
    MySQL 创建库
    brew
    android webview load 本地文件需要注意的地方
    android下隐藏标题栏
    android webview无法加载网页
  • 原文地址:https://www.cnblogs.com/542684416-qq/p/10932192.html
Copyright © 2020-2023  润新知