有道翻译
ssx={'Accept':'application/json,text/javascript,*/*;q=0.01'
}
url2="http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom="
s=input("ss")
dat={
"kw":s
}
ss=requests.post(url2,data=dat,headers=ssx)
print(ss.text)
麻烦大佬看一下
对应的翻译不显示 import requests
import hashlib
import random
import time
print("1.中>>英\n2.英>>中")
while True:
mode = int(input("请输入翻译模式:"))
if mode == 1:
mFrom = "zh-CHS"
mTo = "en"
break
elif mode == 2:
mFrom = "en"
mTo = "zh-CHS"
break
else:
print("输入非法,请重新输入")
text = input("请输入待翻译的内容:")
url = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
headers = {
"Cookie":"OUTFOX_SEARCH_USER_ID=-1347179823@10.110.96.157; OUTFOX_SEARCH_USER_ID_NCOO=254810992.67636687; ___rl__test__cookies=1657200006595",
"Referer":"https://fanyi.youdao.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
lts = str(int(time.time()*1000))
salt = lts + str(random.randint(0,10))
sign_str = "fanyideskweb" + text + salt + "Ygy_4c=r#e#4EX^NUGUc5"
m = hashlib.md5(sign_str.encode())
sign = m.hexdigest()
data = {
"i":text,
"from":mFrom,
"to":mTo,
"smartresult":"dict",
"client":"fanyideskweb",
"salt":salt,
"sign":sign,
"lts":lts,
"bv":"a16a6033635b516a9006542112cdda8f",
"doctype":"json",
"version":"2.1",
"keyfrom":"fanyi.web",
"action":"FY_BY_CLICKBUTTION"
}
response = requests.post(url,headers=headers,data=data)
if response.status_code == 200:
resalut = eval(response.text)["translateResult"]["tgt"]
print("翻译结果:",resalut)
else:
print("翻译失败") import urllib.request
import urllib.parse
s = input("ss")
url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
data = {}
data['i'] = s
data['from'] = 'AUTO'
data['to'] = 'AUTO'
data['smartresult'] = 'dict'
data['client'] = 'fanyideskweb'
data['salt'] = '16377384094512'
data['sign'] = '85efcb3000263b62f1398ac5a8c4da72'
data['lts'] = '1637738409451'
data['bv'] = 'b0ff5d17f404993192085bf8b1e93587'
data['doctype'] = 'json'
data['version'] = '2.1'
data['keyfrom'] = 'fanyi.web'
data['action'] = 'FY_BY_REALTlME'
data = urllib.parse.urlencode(data).encode('utf-8')
response = urllib.request.urlopen(url, data)
html = response.read().decode('utf-8')
print(html) import requests
import hashlib
import time
import random
from jsonpath import jsonpath
import tkinter as tk
class FanYi:
def __init__(self, word, country):
self.url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
'Referer': 'https://fanyi.youdao.com/',
'Cookie': 'OUTFOX_SEARCH_USER_ID=1907954952@182.203.147.49; OUTFOX_SEARCH_USER_ID_NCOO=2032702153.1070416; fanyi-ad-id=306808; fanyi-ad-closed=1; ___rl__test__cookies=1654931146702',
}
self.word = word
self.country = country
def run(self):
lts = int(time.time()*1000)
salt = str(lts) + str(random.randint(0, 9))
sign = hashlib.md5(("fanyideskweb" + self.word + salt + "Ygy_4c=r#e#4EX^NUGUc5").encode()).hexdigest()
data = {
'i': self.word,
'from': 'zh-CHS',
'to': self.country,
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign': sign,
'lts': lts,
'bv': '85618492e851e16ca0a03b8e100fdddb',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME'
}
response = requests.post(self.url, headers=self.headers, data=data).json()
text = jsonpath(response, '$..tgt')
return text
root = tk.Tk()
root.title('简易小翻译')
root.geometry('300x150')
L1 = tk.Label(root, text='请输入要翻译的内容:', font=('宋体', 14)).pack(anchor='nw')
txt = tk.StringVar()
txt.set('')
E1 = tk.Entry(root, textvariable=txt, width=32, bd=3).pack(anchor='nw')
L2 = tk.Label(root, text='翻译后的内容:', font=('宋体', 14)).pack(anchor='nw')
txt2 = tk.StringVar()
def fun1():
word = txt.get()
test = FanYi(word, 'en')
res = test.run()
txt2.set(res)
def fun2():
word = txt.get()
test = FanYi(word, 'ja')
res = test.run()
txt2.set(res)
def fun3():
word = txt.get()
test = FanYi(word, 'ko')
res = test.run()
txt2.set(res)
def fun4():
word = txt.get()
test = FanYi(word, 'ru')
res = test.run()
txt2.set(res)
L3 = tk.Label(root, textvariable=txt2, font=('宋体', 14)).pack(anchor='nw')
B1 = tk.Button(root, text='中->英', command=fun1).pack(anchor='nw')
B2 = tk.Button(root, text='中->日', command=fun2).place(x=60, y=100)
B3 = tk.Button(root, text='中->韩', command=fun3).place(x=120, y=100)
B4 = tk.Button(root, text='中->俄', command=fun4).place(x=180, y=100)
root.mainloop()
-----------------------------------------------------------------------------------------------
有道翻译post请求里的form表单中的salt, sign, lts等几个参数也是随着每次请求不断变化的, 需要做逆向处理,
lts和salt跟时间戳有关, sign则是采用了MD5加密 有加密的
页:
[1]