Python就是爽啊,用來抓網頁信息是再合適不過了。今天結合小甲魚的視頻做了一個簡易翻譯工具,大牛勿噴,僅供娛樂!^_^
主要用到的模塊是json和easyGUI,結合網易有道翻譯的網頁製作而成。效果還可以。
# Powered By KunSoft
# 2015年2月27日
from urllib.request import *
from urllib.parse import *
import easygui as g
import json
while True:
content = g.enterbox(msg='輸入要翻譯的內容', title='簡易翻譯器') #顯示GUI界面,讀入輸入信息
if content == None: #如果點擊關閉按鈕,退出程序
exit(0)
#有道詞典翻譯鏈接
url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=http://www.youdao.com/'
#上傳信息並編碼爲utf-8格式
data = {'type':'AUTO', 'i':content, 'doctype':'json', 'xmlVersion':'1.6', 'keyfrom':'fanyi.web', 'ue':'UTF-8', 'typoResult':'true'}
data = urlencode(data).encode('utf-8')
#打開鏈接,獲取結果,將json格式保存
response = urlopen(url, data)
html = response.read().decode('utf-8')
html = json.loads(html)
#輸出結果並進入下一次翻譯
g.msgbox(msg = html['translateResult'][0][0]['tgt'], title='翻譯結果')