1. 程式人生 > >python 自制有道翻譯工具

python 自制有道翻譯工具

一、登陸有道翻譯頁面,分析請求

url地址:http://fanyi.youdao.com/
在這裡插入圖片描述
綜合上述分析,當改變裡面內容時,Form Data(向後臺傳送的資料)中salt、sign、ts的值在變化,其他幾個沒有變化

二、找到請求的JS

連結地址為:http://shared.ydstatic.com/fanyi/newweb/v1.0.14/scripts/newweb/fanyi.min.js
在這裡插入圖片描述

三、從JS中分析相關內容

在這裡插入圖片描述

四、構造Form Data 資訊

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import requests,time,hashlib,random

def translate(text):
    '''
    加密:sign: n.md5("fanyideskweb" + e + i + "
[email protected]
{h02_BIEe]$P^nG") e:文字內容 i:時間戳 ''' e = text i = int(time.time() * 1000) + random.randint(1, 10) #構建salt r,y = divmod(i,10) #根據salt構建ts,也就是r #構建簽名 sign = "fanyideskweb" + e + str(i) + "[email protected]{h02_BIEe]$P^nG" md5 = hashlib.md5() md5.update(sign.encode('utf-8')) data = { "i": text, "from": "AUTO", "to": "AUTO", "smartresult": "dict", "client": "fanyideskweb", "salt": str(i), "sign": md5.hexdigest(), "ts":str(r), "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "action": "FY_BY_CLICKBUTTION", "typoResult": "false" } url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36", "Referer": "http://fanyi.youdao.com/", "Cookie": "
[email protected]
; OUTFOX_SEARCH_USER_ID_NCOO=878094596.2005905; _ntes_nnid=6783bf0af2ef0809cfacae85efdafde4,1533193798143; JSESSIONID=aaaxTWFTX_yxR8SbjAwEw; ___rl__test__cookies=1544417009120", } response = requests.post(url=url, headers=headers, data=data).json() result = response['translateResult'][0][0]['tgt'] print(result) translate('故鄉')

五、利用tkinter元件構建視覺化視窗

#!/usr/bin/env python
#-*- coding:utf-8 -*-
from tkinter import *
import requests,time,hashlib,random

def translate():
    '''
    加密:sign: n.md5("fanyideskweb" + e + i + "[email protected]{h02_BIEe]$P^nG")
    e:文字內容
    i:時間戳
    '''
    entry1.delete(0, END)
    text = entry.get()


    e = text
    i = int(time.time() * 1000) + random.randint(1, 10)  #構建salt
    r,y = divmod(i,10)    #根據salt構建ts,也就是r

    #構建簽名
    sign = "fanyideskweb" + e + str(i) + "[email protected]{h02_BIEe]$P^nG"
    md5 = hashlib.md5()
    md5.update(sign.encode('utf-8'))

    data = {
        "i": text,
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": str(i),
        "sign": md5.hexdigest(),
        "ts":str(r),
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_CLICKBUTTION",
        "typoResult": "false"
        }

    url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
        "Referer": "http://fanyi.youdao.com/",
        "Cookie": "[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=878094596.2005905; _ntes_nnid=6783bf0af2ef0809cfacae85efdafde4,1533193798143; JSESSIONID=aaaxTWFTX_yxR8SbjAwEw; ___rl__test__cookies=1544417009120",
    }

    response = requests.post(url=url, headers=headers, data=data).json()
    result = response['translateResult'][0][0]['tgt']
    rest.set(result)

def youdao_translate():
    # 建立視窗
    root = Tk()

    # 視窗標題
    root.title("中英互譯")

    # 視窗大小、小寫x
    root.geometry('390x100+500+300')

    # 標籤控制元件
    lable = Label(root, text="請輸入要翻譯的內容:", font=("微軟雅黑"), fg="red")
    lable.grid()
    lablel1 = Label(root, text="翻譯後的內容為:", font=("微軟雅黑"), fg="green")
    lablel1.grid()

    global rest
    rest = StringVar()

    # 輸入控制元件
    global entry
    entry = Entry(root, font=("微軟雅黑", 14))
    entry.grid(row=0, column=1)
    # 翻譯之後的結果
    global entry1
    entry1 = Entry(root, font=("微軟雅黑", 14), textvariable=rest)
    entry1.grid(row=1, column=1)

    # 按鈕控制元件   sticky對齊方式  N S E W - 上下左右
    button = Button(root, text="翻譯", font=("微軟雅黑", 13), command=translate)
    button.grid(row=2, column=0, sticky=W,padx=10)
    button1 = Button(root, text="退出", font=("微軟雅黑", 13), command=root.quit)
    button1.grid(row=2, column=1, sticky=E,padx=10)

    # 訊息迴圈、顯示視窗
    root.mainloop()

if __name__ == '__main__':
    youdao_translate()

在這裡插入圖片描述