python translation interface test - Final

Passed Previous article Inspired by the blog, if we want to achieve Baidu translation, we need to find out where the value of sign comes from, move a small bench, and start a tutorial!

First step

After the translation results come out, we open the developer tool (or press F12), then press "Ctrl+Shift+F" to search globally, search for the keyword "sign", and then click "open"

Click the logo below to format the code

The second step

We still don't know what this sign:y(a) means. Let's set a breakpoint for debugging. Please use Baidu to debug the code.

 

Refresh the page and translate again. After debugging to this line of code, we put the mouse here. You can see that the value of a is the word we enter to query, and the value of sign is probably generated by the function y, so what is y? When we put the mouse on y, we can see the main body of the function. Click the mouse to enter the main body of the function

The code of the function body is as follows:

function e(r) {
    var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
    if (null === o) {
        var t = r.length;
        t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
    } else {
        for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)
            "" !== e[C] && f.push.apply(f, a(e[C].split(""))),
            C !== h - 1 && f.push(o[C]);
        var g = f.length;
        g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
    }
    var u = void 0,
        l = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
    u = null !== i ? i : (i = window[l] || "") || "";
    for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
        var A = r.charCodeAt(v);
        128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)),
                    S[c++] = A >> 18 | 240,
                    S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224,
                S[c++] = A >> 6 & 63 | 128),
            S[c++] = 63 & A | 128)
    }
    for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++)
        p += S[b],
        p = n(p, F);
    return p = n(p, D),
        p ^= s,
        0 > p && (p = (2147483647 & p) + 2147483648),
        p %= 1e6,
        p.toString() + "." + (p ^ m)
}

The third step

We save the js code as fun_e(r).js, and then use Python's PyExecJS library to debug and run the js code (you need to install the node.js environment in advance)

import execjs
# print(execjs.get().name) # Output environment name
with open("pytest\Translation interface\\fun_e(r).js", "r", encoding='utf-8') as f:
    ctx = execjs.compile(f.read())
print(ctx.call("e", "I'm a handsome man"))

But the post run prompt variable i is not defined

We go back to the browser, find i, and set a breakpoint as well. Here i set a breakpoint for the variable u, which is convenient to see the change of the value of the variable u. as above, after debugging, we can see that the value of i is "320305.131321201"

At this time, we are not sure whether the value of i is "320305.131321201". We need to change the word for translation debugging again. After many tests, the value of i is fixed. We can confirm that the value of i is "320305.131321201" (string type)

The fourth step

We define an i variable in the js file we built before and assign a value to it

Re debugging and running prompts that n is not defined

We do the same thing, return to the browser, search for N, interrupt debugging, and find the function body of n

Let's copy this code and put it in front of the js file and rerun the code

Get the sign value! (if you are not sure whether the sign value is the same, search for the same word in the browser to see if the sign value of the submitted form is the same. If you change the word several times, you can confirm that this is the sign value.)

The complete js code is as follows

function n(r, o) {
    for (var t = 0; t < o.length - 2; t += 3) {
        var a = o.charAt(t + 2);
        a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
            a = "+" === o.charAt(t + 1) ? r >>> a : r << a,
            r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
    }
    return r
}

function e(r) {
    var o = r.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
    if (null === o) {
        var t = r.length;
        t > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(t / 2) - 5, 10) + r.substr(-10, 10))
    } else {
        for (var e = r.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), C = 0, h = e.length, f = []; h > C; C++)
            "" !== e[C] && f.push.apply(f, a(e[C].split(""))),
            C !== h - 1 && f.push(o[C]);
        var g = f.length;
        g > 30 && (r = f.slice(0, 10).join("") + f.slice(Math.floor(g / 2) - 5, Math.floor(g / 2) + 5).join("") + f.slice(-10).join(""))
    }
    var i = "320305.131321201";
    var u = void 0,
        l = "" + String.fromCharCode(103) + String.fromCharCode(116) + String.fromCharCode(107);
    u = null !== i ? i : (i = window[l] || "") || "";
    for (var d = u.split("."), m = Number(d[0]) || 0, s = Number(d[1]) || 0, S = [], c = 0, v = 0; v < r.length; v++) {
        var A = r.charCodeAt(v);
        128 > A ? S[c++] = A : (2048 > A ? S[c++] = A >> 6 | 192 : (55296 === (64512 & A) && v + 1 < r.length && 56320 === (64512 & r.charCodeAt(v + 1)) ? (A = 65536 + ((1023 & A) << 10) + (1023 & r.charCodeAt(++v)),
                    S[c++] = A >> 18 | 240,
                    S[c++] = A >> 12 & 63 | 128) : S[c++] = A >> 12 | 224,
                S[c++] = A >> 6 & 63 | 128),
            S[c++] = 63 & A | 128)
    }
    for (var p = m, F = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(97) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(54)), D = "" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(51) + ("" + String.fromCharCode(94) + String.fromCharCode(43) + String.fromCharCode(98)) + ("" + String.fromCharCode(43) + String.fromCharCode(45) + String.fromCharCode(102)), b = 0; b < S.length; b++)
        p += S[b],
        p = n(p, F);
    return p = n(p, D),
        p ^= s,
        0 > p && (p = (2147483647 & p) + 2147483648),
        p %= 1e6,
        p.toString() + "." + (p ^ m)
}

The fifth step

Since how to get the sign value, baidu translation will be done, with the complete code

# Decipher sign value
import requests
import re
import execjs 
import json

headers = {
    "User-Agent":
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
    'Cookie':
        'PSTM=1575539987; BAIDUID=A80872D93185A688C8AC8CF159A4EF54:FG=1; BIDUPSID=3D8B9E5934B26CCEF003426B6773A431; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1455_21114_26350_30717; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; APPGUIDE_8_2_2=1; from_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; to_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1580901474,1580904731,1580905304,1580906217; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1580906217; yjs_js_security_passport=0fdd751f0722a3d0fb6a389e7fe2c2f2c9864c58_1580906237_js; __yjsv5_shitong=1.0_7_4a4fb445c2fea6132d704ca5e45285f82f61_300_1580906238325_223.91.220.158_37acdb4d; delPer=0; PSINO=1'
}

form_data = {
    # 'from': 'zh',
    # 'to': 'en',
    # 'query': word,
    'transtype': 'realtime',
    'simple_means_flag': '3',
    # 'sign': None,
    'token': '42e08df4cfc832413f5c96516ed21cc3'
}

# Get the value of sign
def getSign(word):
    with open('pytest\Translation interface\\fun_e(r).js', 'r', encoding='utf-8') as f:
        sign_js = execjs.compile(f.read())
        return sign_js.call('e', word)

# Get the json data of the result
def getResult(url):
    try:
        r = requests.post(url, data=form_data, headers=headers, timeout=30)
        r.raise_for_status
        return r.text
    except:
        return ''
# Main function
print("This procedure supports both Chinese English translation and English Chinese Translation")
print("Please input 1 for Chinese-English translation and other non-zero numbers for English-Chinese Translation")
print("To terminate the program, enter 0")
while(True):
    a = eval(input("Please enter a number:"))
    if a == 1:
        f = 'zh'
        t = 'en'
    elif a==0:
        break
    else:
        f = 'en'
        t = 'zh'
    url_post = "https://fanyi.baidu.com/v2transapi"
    word = input("Please enter the characters to query:")
    sign = getSign(word)
    form_data['from'] = f
    form_data['to'] = t
    form_data['query'] = word
    form_data['sign'] = sign
    text = getResult(url_post)
    json_data = json.loads(text)
    # print(json_data)
    trans_result = json_data['trans_result']['data'][0]['dst']
    # print(trans_result)
    print("{}The translation of is:{}".format(word, trans_result))
print("Program exit!")

The operation results are as follows

143 original articles published, 78 praised, 40000 visited+
Private letter follow

Tags: JSON encoding Python Windows

Posted on Sat, 15 Feb 2020 00:08:50 -0800 by melittle