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
| import requests import json import time import random import hashlib import re url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
jsurl = "http://shared.ydstatic.com/fanyi/newweb/v1.0.27/scripts/newweb/fanyi.min.js" jsContent = str(requests.post(url=jsurl).content,"utf8")
jsContent = jsContent.replace("+","") password = re.findall(r'"fanyideskweb"ei"(.*?)"',jsContent) word = input("输入翻译词:") while(word != "#"): timestamp = str(int(time.time()*1000) + random.randint(0,10)) sign = "fanyideskweb" + word + timestamp + password[0] md5 = hashlib.md5() md5.update(sign.encode("utf8")) signMd5 = md5.hexdigest() formatData = { "i": word, "from": "AUTO", "to": "AUTO", "smartresult": "dict", "client": "fanyideskweb", "salt": str(timestamp), "sign": signMd5, "doctype": "json", "version": "2.1", "keyfrom": "fanyi.web", "action": "FY_BY_REALTlME", } headers = { "Cookie": "[email protected]; JSESSIONID=aaaVuQquabmk44VgVY3kx; OUTFOX_SEARCH_USER_ID_NCOO=1742625287.947354; ___rl__test__cookies=1592221619433", "Host": "fanyi.youdao.com", "Origin":"http://fanyi.youdao.com", "Referer":"http://fanyi.youdao.com/", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" } response = requests.post(url=url,data=formatData,headers=headers) result = json.loads(str(response.content,"utf8")) print("输入的字符为:{}".format(result['translateResult'][0][0]['src'])) print("结果为:{}".format(result['translateResult'][0][0]['tgt'])) print("------------") word = input("输入翻译词:")
|