-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
82 lines (73 loc) · 2.68 KB
/
translator.py
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
78
79
80
81
82
# -*- coding: utf-8 -*-
import requests
import hashlib
import time
import json
import random
class Youdao(object):
def __init__(self, msg):
self.msg = msg
self.url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
self.D = "Ygy_4c=r#e#4EX^NUGUc5"
self.salt = self.get_salt()
self.sign = self.get_sign()
def get_md(self, value):
'''md5加密'''
m = hashlib.md5()
# m.update(value)
m.update(value.encode('utf-8'))
return m.hexdigest()
def get_salt(self):
'''根据当前时间戳获取salt参数'''
s = int(time.time() * 1000) + random.randint(0, 10)
return str(s)
def get_sign(self):
'''使用md5函数和其他参数,得到sign参数'''
s = "fanyideskweb" + self.msg + self.salt + self.D
return self.get_md(s)
def get_result(self):
'''headers里面有一些参数是必须的,注释掉的可以不用带上'''
headers = {
# 'Accept': 'application/json, text/javascript, */*; q=0.01',
# 'Accept-Encoding': 'gzip, deflate',
# 'Accept-Language': 'zh-CN,zh;q=0.9,mt;q=0.8',
# 'Connection': 'keep-alive',
# 'Content-Length': '240',
# 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': '[email protected];',
# 'Host': 'fanyi.youdao.com',
# 'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:51.0) Gecko/20100101 Firefox/51.0',
# 'X-Requested-With': 'XMLHttpRequest'
}
data = {
'i': self.msg,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': self.salt,
'sign': self.sign,
'lts':self.sign[:-1],
'bv':'be559818a402acf44a9a990b7ef68fe9',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_CL1CKBUTTON',
'typoResult': 'true'
}
html = requests.post(self.url, data=data, headers=headers).text
# print(html)
infos = json.loads(html)
if 'translateResult' in infos:
try:
result = infos['translateResult'][0][0]['tgt']
# print(result)
return result
except:
return None
pass
if __name__ == '__main__':
y = Youdao('你是我的小苹果,我是你的优乐美')
print(y.get_result())