zoukankan      html  css  js  c++  java
  • 免费翻译API破解(简易翻译工具)

     思路:选取有道翻译,用fiddler抓取接口请求信息,提取相关请求参数,破解加密部分。

    主要请求数据:

    i  :翻译文本

    ts:时间戳    

    salt:ts +随机数

    sign:加密信息,经过抓取信息,发现sign = md5(固定字符串1+ i + salt +固定字符串2)  固定字符串请百度方法。

    数据经过urlencode后请求,获取到json数据后取出翻译文本。

    下面直接上代码:

    #coding = utf-8
    import requests
    from urllib import parse
    from hashlib import md5
    import hashlib
    import time,random
    
    class YouDaofanyi(object):
    
        def __init__(self,text):
    
            self.url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
            self.method = "POST"
            self.headers = {
                            "Origin": "http://fanyi.youdao.com",
                            "Referer": "http://fanyi.youdao.com/",
                            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134",
                            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                            "Accept": "application/json; text/javascript; */*; q=0.01",
                            "X-Requested-With": "XMLHttpRequest",
                            "Accept-Language": "zh-CN",
                            "Accept-Encoding": "gzip; deflate",
                            "Connection": "Keep-Alive",
                            "host" : "fanyi.youdao.com",
                            "Cookie": "xxxxxxxx"
                            }
    
            self.text = text
            utc =  int(time.time() * 1000)
            salt = utc*10 + random.randint(0,11)
    
            #print(utc,salt)
            signstr = ("fanyideskweb" + str(text) + str(salt) +"rY0D^0'nM0}g5Mm1z%1G4").encode("utf-8")
            md5 = hashlib.md5()
            md5.update(signstr)
            sign = md5.hexdigest()
    
            print("input text is       : %s" % text)
            post_data ={"i":text,
                       "smartresult":"dict",
                       "from":"AUTO",
                       "to":"AUTO",
                       "client":"fanyideskweb",
                       "salt":salt,
                       "sign":sign,
                       "ts":str(utc),
                       "bv":"435192ea8debc53c68e5c13d953099ff",
                       "doctype":"json",
                       "version":2.1,
                       "keyfrom":"fanyi.web",
                       "action":"FY_BY_REALTIME",
                       "typoResult":"false"
                       }
            self.data = parse.urlencode(post_data)
         
        def translate(self):
            test = requests.post(url = self.url,data= self.data,headers =self.headers)
            resultjson = test.json()
    
            if resultjson == {'errorCode': 50}:
                translate = ""
            else:
                try:
                    translate = resultjson["translateResult"]
                    translate = translate[0][0]["tgt"]
                    print("translate result is : %s" % translate)
                except KeyError as f:
                    translate =""
    
            return translate
    

      

    后面想到做一个小工具能翻译文本内容,又能翻译excel档文件,马上实现了,这里把翻译文本直接追加到对应的单元格。

    #coding=utf-8
    
    import xlrd,xlwt
    from xlrd import *
    import Translate
    from xlutils.copy import copy
    
    def TypeCheck(typename):
        flag = False
        if typename == 0:
            print("单元格内容为空")
        elif typename ==1:
            print("字符串,翻译中...")
            flag = True
        elif typename == 2:
            print("单元格为数字")
        elif typename ==3:
            print ("单元格为日期")
        elif typename == 4:
            print("单元格为布尔类型")
        else:
            print("格式获取错误")
        return flag
    
    
    def Excelread(filename):
        rd = xlrd.open_workbook(filename)
        sheet = rd.sheet_by_index(0)
        readtext = []
        readalltext =[]
        for i in range(1000):
            for j in range(1000):
                try:
                    valuetype = sheet.cell(i,j).ctype
                    resulttype = TypeCheck(valuetype)
                    if resulttype== False:
                        print("不需要进行翻译的内容")
                    else:
                        value = sheet.cell_value(i, j)
                        translatevalue = Translate.translate(value)
                        newvalue = str(value) + translatevalue
                        ExcelWrite(filename,i,j,newvalue)
                        readtext.append(value)
                except IndexError as f:
                    pass
            readalltext.append(readtext)
            readtext = []
        flag = False
        for i in range(len(readalltext)):
            if readalltext[i] != []:
                flag = True
                break
            else:
                pass
        return flag
    
    def ExcelWrite(filename,raw,cow,value):
        rb = xlrd.open_workbook(filename)
        wt = copy(rb)
        s = wt.get_sheet(0)
        s.write(raw,cow,value)
        wt.save(filename)
    

      

    再加上tk部分代码:

        try:
            root = Tkinter.Tk()
            root.title("翻译工具")
            root.geometry("850x350")
    
            root.resizable(width=False, height=False)
            Tkinter.Label(root, text='有道翻译区域', fg='blue', font=("黑体", 20, "bold")).grid(row=0, column=2)
            Tkinter.Label(root, text='*Version: v0.1-20190329*', fg='blue', font=("黑体", 10)).grid(row=11, column=4)
            Tkinter.Label(root, text='*Design : Sandy1128*', fg='blue', font=("黑体", 10)).grid(row=12, column=4)
    
            Tkinter.Label(root,text='需要翻译的文本:',font=("黑体", 10)).grid(row=1,column=1)
            fanyitext_t=Tkinter.Text(root,height=5,width=60, fg='blue')
            fanyitext_t.grid(row=1,column=2)
            fanyitext_t.insert('0.0','123')
    
            Tkinter.Label(root, text='或需要翻译的文件路径:',font=("黑体", 10)).grid(row=2, column=1)
            fanyifname_t = Tkinter.Text(root, height=5, width=60, fg='blue')
            fanyifname_t.grid(row=2, column=2)
    
            fanyiinputfile =Tkinter.Button(root, text='浏览',activeforeground = "red",fg='blue',font=("黑体", 12, "bold"), height =2,command= selectfilefanyi)
            fanyiinputfile.grid(row = 2,column = 3)
    
            btn_fanyi=Tkinter.Button(root,text='开始翻译',activeforeground = "red",fg='black',font=("黑体", 12, "bold"),height = 2,command=qidongfanyiapp)
            btn_fanyi.grid(row=5,column=3)
    
            root.mainloop()
    
        except Exception as e:
            pass
    

    视图:

    可以将python代码打包成exe文件,方便使用。

  • 相关阅读:
    TSQL查询进阶深入理解子查询
    CodeSmith和PowerDesigner的安装和数据库创建
    Inten对象中的Flag
    JNI配置问题
    Android技巧篇
    onSaveInstanceState状态问题
    Android MMSTransactionService
    Android MMS
    AcctivityManager
    隐藏键盘
  • 原文地址:https://www.cnblogs.com/Sandy-1128/p/fanyi-sandy-0329.html
Copyright © 2011-2022 走看看