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
| import urllib.request import urllib.parse import json
headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36' } url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
key = input('请输入你要翻译的内容:')
data = { 'i': key, 'from': 'AUTO', 'to': 'AUTO', 'smartresult': 'dict', 'client': 'fanyideskweb', 'salt': '16025076981759', 'sign': 'b4f4f6b09fa355131e184201607498b1', 'lts': '1602507698175', 'bv': '4abf2733c66fbf953861095a23a839a8', 'doctype': 'json', 'version': '2.1', 'keyfrom': 'fanyi.web', 'action': 'FY_BY_REALTlME' } data = urllib.parse.urlencode(data).encode('utf-8') data = bytes(data)
req = urllib.request.Request(url,data=data,headers=headers) res = urllib.request.urlopen(req) html = res.read().decode('utf-8')
r_dict = json.loads(html) print(r_dict,type(r_dict)) print(r_dict['translateResult'][0][0]['tgt'])
|