Python逆向网易有道翻译练习代码

本文借助网易有道翻译网站进行逆向学习,实现输入原文,得到输出译文的目的。本文只有代码,分析过程略过

参考资料

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import time
from hashlib import md5

import requests


class YouDao():

@staticmethod
def timestamp():
'''
时间戳
'''
t = time.time()
timestamp_length_13 = int(t * 1000)
timestamp_length_14 = int(t * 10000)
return timestamp_length_13, timestamp_length_14

@staticmethod
def set_md5_encode():
'''
将字符串MD5加密
'''
m = md5()
timestamp_length_13, timestamp_length_14 = YouDao.timestamp()
text = input('输入翻译的文字:')
str = f'fanyideskweb{text}{timestamp_length_14}Y2FYu%TNSbMCxc3t2u^XT'
m.update(str.encode())
sign = m.hexdigest()
return sign, text, timestamp_length_13, timestamp_length_14

@staticmethod
def main():
sign, text, timestamp_length_13, timestamp_length_14 = YouDao.set_md5_encode()
url = 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Connection': 'keep-alive',
'Content-Length': '237',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': f'OUTFOX_SEARCH_USER_ID_NCOO=683345340.4698274; OUTFOX_SEARCH_USER_ID="-1230032020@10.105.137.203"; _ga=GA1.2.39900781.1644214364; JSESSIONID=aaar_k4pyqlIU4yeehB8x; DICT_LOGIN=8||1645432450949; DICT_FORCE=true; ___rl__test__cookies={timestamp_length_13}',
'Host': 'fanyi.youdao.com',
'Origin': 'https://fanyi.youdao.com',
'Referer': 'https://fanyi.youdao.com/',
'sec-ch-ua': 'Not A;Brand";v="99", "Chromium";v="98", "Microsoft Edge";v="98',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.56',
'X-Requested-With': 'XMLHttpRequest',
}

data = {
'i': text,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': timestamp_length_14,
'sign': sign,
'lts': timestamp_length_13,
'bv': '40de26f77140584455ad2da449820e0c',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME',
}

response = requests.post(url, headers=headers, data=data)
print(response.json())


YouDao.main()

输出

1
2
输入翻译的文字:blog
{'translateResult': [[{'tgt': '博客', 'src': 'blog'}]], 'errorCode': 0, 'type': 'en2zh-CHS', 'smartResult': {'entries': ['', 'n. 网络日志,博客\r\n', 'v. 写博客\r\n'], 'type': 1}}