我是靠谱客的博主 柔弱太阳,最近开发中收集的这篇文章主要介绍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()

最后

以上就是柔弱太阳为你收集整理的Python-基于有道翻译的简单文本翻译器的全部内容,希望文章能够帮你解决Python-基于有道翻译的简单文本翻译器所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部