有道翻译
import hashlib
import random
import time
import requests
keyword = input('请输入你要查询的内容:')
def youdao_trans(keyword):
salt = str(int(time.time()) * 1000 + random.randint(0, 10))
left = 'fanyideskweb'
e = keyword # 翻译的词,图中的e
i = salt
# right = 'Tbh5E8=q6U3EXe+&L[4c@'
right = "Y2FYu%TNSbMCxc3t2u^XT"
str_data = left + e + i + right
m = hashlib.md5()
m.update(str_data.encode('utf-8'))
sign = m.hexdigest()
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36',
'Referer': 'http://fanyi.youdao.com/',
'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=920530045.7161942; OUTFOX_SEARCH_USER_ID="1506261127@10.108.160.17"; _ga=GA1.2.1288125049.1599879702; _ntes_nnid=7dc5dc11be36895d173bef7fe1b51cb1,1606305443534; JSESSIONID=aaaAC5o5FTuNsgXAdm7Dx; ___rl__test__cookies=1612686603098'
}
data = {
'i': keyword,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': salt,
'sign': sign,
'lts': str(int(time.time() * 1000)),
'bv': 'a5aea452e07ae89289929c1ac945c792',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME',
}
html = requests.post(url=url, headers=headers, data=data).json()
translation = html['translateResult'][0][0]['tgt']
print(f'单词:{keyword}')
print(f'释义:{translation}')
youdao_trans(keyword)