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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import requests import execjs import os import sys
cookies = { 'OUTFOX_SEARCH_USER_ID': '-39140274@218.106.117.234', 'OUTFOX_SEARCH_USER_ID_NCOO': '1017813054.5103312', 'DICT_DOCTRANS_SESSION_ID': 'NGI5MmMzZTktYmY5ZS00Njg1LTkwODMtODZlZjI2OTA5YzZh', '_uetsid': 'b1a3a4202e6211f09ed8910f851b0f84', '_uetvid': '0a3206b0b5e211ef97071f44ad9eeb40', }
headers = { 'Accept': 'application/json, text/plain, */*', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ar;q=0.5', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'DNT': '1', 'Origin': 'https://fanyi.youdao.com', 'Pragma': 'no-cache', 'Referer': 'https://fanyi.youdao.com/', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0', 'sec-ch-ua': '"Chromium";v="136", "Microsoft Edge";v="136", "Not.A/Brand";v="99"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Windows"', }
def youdao(txt): jscode = execjs.compile(open("translate.js", encoding="utf-8").read()) time_now,sign = jscode.call("getsign") data = { 'i': txt, 'from': 'zh-CHS', 'to': 'en', 'useTerm': 'false', 'domain': '0', 'dictResult': 'true', 'keyid': 'webfanyi', 'sign': sign, 'client': 'fanyideskweb', 'product': 'webfanyi', 'appVersion': '1.0.0', 'vendor': 'web', 'pointParam': 'client,mysticTime,product', 'mysticTime': time_now, 'keyfrom': 'fanyi.web', 'mid': '1', 'screen': '1', 'model': '1', 'network': 'wifi', 'abtest': '0', 'yduuid': 'abcdefg', } response = requests.post('https://dict.youdao.com/webtranslate', cookies=cookies, headers=headers, data=data.encode('utf-8')).text
result = jscode.call('translate_response',response) translate = result['translateResult'][0][0]['tgt'] print(f'翻译结果为:{translate}')
if __name__ == '__main__': txt = input('请输入要翻译的内容:') youdao(txt)
|