import urllib.request
import urllib.parse
import json
keyword = input("请输入要翻译的中文:")
url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36'
}
data = {
'i': keyword,
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': '16477477581351',
'sign': 'f5abb1c4dffe6868fdddfd4b8a14e44a',
'lts': '1647747758135',
'bv': 'c2777327e4e29b7c4728f13e47bde9a5',
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME',
}
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url, data=data, headers=headers)
res = urllib.request.urlopen(req).read().decode('utf-8')
object = json.loads(res)
print(object['translateResult'][0][0]['tgt'])