-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyoudao.py
66 lines (50 loc) · 1.76 KB
/
youdao.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
#!/usr/bin/env python
# coding=utf-8
import urllib.request
import urllib.parse
import time
import random
import hashlib
import sys
def tranlate(argv):
argc = len(argv)
sentence = ''
if(argc <= 1):
sentence = "人生苦短, 你需要Python!!"
for i in range(1, argc):
sentence = sentence + argv[i]
url = r'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule&sessionFrom=www.baidu.com'
data = {
'from':'AUTO',
'to':'AUTO',
'smartresult':'dict',
'client':'fanyideskweb',
'doctype':'json',
'version':'2.1',
'keyfrom':'fanyi.web',
'action':'FY_BY_CLICKBUTTION',
'typoResult':'true'
}
## #get data from javascript
## para = gen_salt_sign(key)
salt = str(int(time.time() * 1000) + random.randint(0, 9))
sign = "fanyideskweb" + sentence + salt + "rY0D^0'nM0}g5Mm1z%1G4"
sign = hashlib.md5(sign.encode('utf-8')).hexdigest()
data['i'] = sentence
data['salt'] = salt
data['sign'] = sign
headers = {
'Host':'fanyi.youdao.com',
'Origin':'http://fanyi.youdao.com',
'Pragma':'no--cache',
'Referer':'http://fanyi.youdao.com/',
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url = url, data = data, headers = headers, method = 'POST')
#request = urllib.request.Request(url = url, data = data, method = 'POST')
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
if __name__ == '__main__':
tranlate(sys.argv)