1. import urllib.request
    2. import urllib.parse
    3. import json
    4. keyword = input("请输入要翻译的中文:")
    5. url = 'https://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    6. headers = {
    7. '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'
    8. }
    9. data = {
    10. 'i': keyword,
    11. 'from': 'AUTO',
    12. 'to': 'AUTO',
    13. 'smartresult': 'dict',
    14. 'client': 'fanyideskweb',
    15. 'salt': '16477477581351',
    16. 'sign': 'f5abb1c4dffe6868fdddfd4b8a14e44a',
    17. 'lts': '1647747758135',
    18. 'bv': 'c2777327e4e29b7c4728f13e47bde9a5',
    19. 'doctype': 'json',
    20. 'version': '2.1',
    21. 'keyfrom': 'fanyi.web',
    22. 'action': 'FY_BY_REALTlME',
    23. }
    24. data = urllib.parse.urlencode(data).encode('utf-8')
    25. req = urllib.request.Request(url, data=data, headers=headers)
    26. res = urllib.request.urlopen(req).read().decode('utf-8')
    27. object = json.loads(res)
    28. print(object['translateResult'][0][0]['tgt'])