Skip to content

Commit b240e8e

Browse files
committed
有道
1 parent 392b3bf commit b240e8e

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import requests
2+
import hashlib
3+
import time
4+
import urllib.parse
5+
# 创建md5对象
6+
def nmd5(str):
7+
m = hashlib.md5()
8+
# Tips
9+
# 此处必须encode
10+
# 若写法为m.update(str) 报错为: Unicode-objects must be encoded before hashing
11+
# 因为python3里默认的str是unicode
12+
# 或者 b = bytes(str, encoding='utf-8'),作用相同,都是encode为bytes
13+
b = str.encode(encoding='utf-8')
14+
m.update(b)
15+
str_md5 = m.hexdigest()
16+
return str_md5
17+
def formdata(transtr):
18+
# 待加密信息
19+
headerstr = '5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
20+
#print(round(time.time()*1000))
21+
bv=nmd5(headerstr)
22+
ts=str(round(time.time()*1000))
23+
salt=ts+'90'
24+
strexample='fanyideskweb'+transtr+salt+'n%A-rKaT5fb[Gy?;N5@Tj'
25+
sign=nmd5(strexample)
26+
#print(sign)
27+
i=len(transtr)
28+
#print(i)
29+
# print('MD5加密前为 :' + headerstr)
30+
# print('MD5加密后为 :' + bv)
31+
dict={'i':transtr,'from':'AUTO','TO':'AUTO','smartresult': 'dict',
32+
'client':'fanyideskweb',
33+
'salt':salt,
34+
'sign':sign,
35+
'ts':ts,
36+
'bv':bv,
37+
'doctype':'json',
38+
'version':'2.1',
39+
'keyfrom':'fanyi.web',
40+
'action':'FY_BY_REALTlME'
41+
}
42+
return dict
43+
44+
45+
url='http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
46+
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
47+
'Referer':'http://fanyi.youdao.com/',
48+
'Origin': 'http://fanyi.youdao.com',
49+
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
50+
'X-Requested-With':'XMLHttpRequest',
51+
'Accept':'application/json, text/javascript, */*; q=0.01',
52+
'Accept-Encoding':'gzip, deflate',
53+
'Accept-Language':'zh-CN,zh;q=0.9',
54+
'Connection': 'keep-alive',
55+
'Host': 'fanyi.youdao.com',
56+
'cookie':'_ntes_nnid=937f1c788f1e087cf91d616319dc536a,1564395185984; OUTFOX_SEARCH_USER_ID_NCOO=; [email protected]; JSESSIONID=; ___rl__test__cookies=1'
57+
}
58+
input=input("请输入翻译内容:")
59+
dict=formdata(input)
60+
dict=urllib.parse.urlencode(dict)
61+
dict=str(dict)
62+
#dict=urllib.parse.urlencode(dict).encode('utf-8')
63+
64+
req=requests.post(url,data=dict,headers=header)
65+
val=req.json()
66+
print(val['translateResult'][0][0]['tgt'])

0 commit comments

Comments
 (0)