Python 基於有道翻譯的簡單文字翻譯器

2021-09-29 12:46:34 字數 2539 閱讀 4244

基於有道翻譯的簡單文字翻譯器

import requests

from bs4 import beautifulsoup

import tkinter as tk

from tkinter import ttk

import re

defgetresult

(sentin,form)

: headers =

post_data =

post_url =

""r = requests.post(post_url, data=post_data, headers=headers)

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 =

deftranslate()

: getcom = com.get(

) sentin = entryin.get(

) form = corresponding.get(getcom)

last = getresult(sentin, form)

result.insert(

0,last)

defclear()

: entryin.delete(0,

'end'

) result.delete(0,

'end'

)roottrans=tk.tk(

)roottrans.title(

'login'

)roottrans[

'height']=

300roottrans[

'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.strin**ar(

)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版有道翻譯

做過爬蟲的朋友應該很通俗易懂,話不多說 亮 吧。需要用到的模組 urllib time hashlib random json code encoding utf 8 author search plugname youdaodict import urllib.request import url...

python 嘗試有道翻譯

乙隻小白的爬蟲,寫了乙個簡單 有道翻譯 記錄一下,如果大家有更好的方式 方法記得分享一下哦!coding utf 8 import urllib,urllib2,json url 有道翻譯 def translation word data data urllib.urlencode data en...

python請求有道翻譯

import requests import time,hashlib,math,random import pprint from fake useragent import useragent ua useragent random defget from word r math.floor t...