Python-基于有道翻译的简单文本翻译器

基于有道翻译的简单文本翻译器
import requests
from bs4 import BeautifulSoup
import tkinter as tk
from tkinter import ttk
import re

def getResult(SentIn,form):
    headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
    }

    post_data = {
        "inputtext": SentIn,
        "type": form
    }

    post_url = "http://m.youdao.com/translate"
    r = requests.post(post_url, data=post_data, headers=headers)
    r.encoding = r.apparent_encoding
    Demo = r.text
    soup = BeautifulSoup(Demo, "html.parser")
    getLi = str(soup.find_all(id="translateResult")[0].li)
    end = getLi[4:len(getLi)-5:]
    return end

#Type对应的翻译关系
Corresponding = {
    "中译英":"ZH_CN2EN",
    "中译日":"ZH_CN2JA",
    "中译韩":"ZH_CN2KR",
    "中译法":"ZH_CN2FR",
    "中译俄":"ZH_CN2RU",
    "中译西":"ZH_CN2SP",
    "英译中":"EN2ZH_CN",
    "日译中":"JA2ZH_CN",
    "韩译中":"KR2ZH_CN",
    "法译中":"FR2ZH_CN",
    "俄译中":"RU2ZH_CN",
    "西译中":"SP2ZH_CN"
}

def translate():
    getcom = com.get()
    SentIn = entryIn.get()
    form = Corresponding.get(getcom)
    Last = getResult(SentIn, form)
    Result.insert(0,Last)

def clear():
    entryIn.delete(0,'end')
    Result.delete(0, 'end')

rootTrans=tk.Tk()
rootTrans.title('Login')
rootTrans['height']=300
rootTrans['width']=350

#标签
labelName=tk.Label(rootTrans,font="GB-2312",text="基于有道翻译的翻译软件",)
labelName.place(x=70,y=30,height=30)
labelName=tk.Label(rootTrans,text='需要翻译的词句:')
labelName.place(x=10,y=90,width=100,height=30)
labelName=tk.Label(rootTrans,text='翻译结果:')
labelName.place(x=10,y=200,width=100,height=30)

#输入
entryIn=tk.Entry(rootTrans,width=150)
entryIn.place(x=110,y=90,width=200,height=30)
Result=tk.Entry(rootTrans,width=150)
Result.place(x=110,y=200,width=200,height=30)

#按钮
buttonTrans=tk.Button(rootTrans,text='翻译',command = translate)
buttonTrans.place(x=200,y=150,width=50,height=30)
buttonTrans=tk.Button(rootTrans,text='清空',command = clear)
buttonTrans.place(x=280,y=150,width=50,height=30)

#下拉框
xVariable = tk.StringVar()
com = ttk.Combobox(rootTrans,textvariable=xVariable)
com.place(x=50,y=150,width=100,height=30)
com["value"] = ("中译英","中译日","中译韩","中译法","中译俄","中译西","英译中","日译中","韩译中","法译中","俄译中","西译中")
com.current(0)

rootTrans.mainloop()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值