仅能翻译英文,中文可以换个接口自己写 如有不懂,可私信
# -*- coding: utf-8 -*-
# day day study day day up
# create by a Man
import requests,json5,execjs,time
def sumtime(f):
def test(word):
begin_time=time.time()
f(word)
end_time=time.time()
print(f'程序执行时间为:{end_time-begin_time}s')
return test
@sumtime
def fan(word):
url='https://fanyi.baidu.com/v2transapi?'
header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
'Cookie': 'BAIDUID=19D0790B1844DD621B03582389AEBD89:FG=1; BIDUPSID=19D0790B1844DD621B03582389AEBD89; PSTM=1627419641; __yjs_duid=1_25483b20e2247361951aef06981aa0f21627483425257; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDRCVFR[oxw6P5LCmIs]=mk3SLVN4HKm; delPer=0; H_PS_PSSID=31660_26350; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1630495219,1630676395,1632231565; PSINO=6; BDRCVFR[IuwK-wiJ3L3]=K7PavkeTFPTUAN8ULuEQhPEUi4WU6; __yjs_st=2_NmM2NWQzMTc2NWNjMTQyZGQ2YWI0YTBjYmE5OTFiY2U5MzExMjZiNmRkZTA1ODgwMmJhOGJlYzhjMGRjNmUxYTRiYWRhNzVhN2RjMjBkN2YwYTU1OTcwZDc3OGM5Mjk3OTQ3MzQ0MDk2YTAxZDUzNDIzYTM2ZWJkZWE5ZTczYTA1M2VmMDkzYzg4MmMzYWMyNWIwMDcxYTc2MGI0ZjlmYTA1NWRmYTI0NTI2YzgzNmEzMDc3NzkxMmMyZDEyYjgxN2QyODI1Yzc4NmY4M2IyNWIwNzk2OWQzMjA1MDViMjRiOGNlMDg4MjM4ZmYzNWQ1MDAzYWJjZDBmOTM2NjEyY183Xzc5YjMyMzA1; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1632540724'}
with open ('bd_fanyi.js',encoding='utf-8') as f:
code = f.read()
ctx = execjs.compile(code)
pwd=ctx.eval(f'e("{word}")')
data={
'from': 'en',
'to': 'zh',
'query': word,
'transtype': 'enter',
'simple_means_flag': 3,
'sign': pwd,
'token': '8c364cb902b3b6a12ea529452ea126b9',
'domain': 'common',}
res=requests.post(url=url,headers=header,data=data).text
try:
result_word=json5.loads(res)['trans_result']['data'][0]['dst']
print(result_word)
result_word=json5.loads(res)['dict_result']['simple_means']['word_means']
print(f'查询结果:{str(result_word).replace("[","").replace("]","")}')
except:
pass
word=input("请输入需要翻译的English:")
fan(word)
bd_fanyi.js
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 = "320305.131321201" || "") || "";
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)
}
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
}
var i = null;