python 有道翻譯
阿新 • • 發佈:2018-12-03
import requests import time import random import hashlib i = str(int(time.time()*1000)+random.randint(1,10)) t = input("please input the word you want to translate:\n") u = 'fanyideskweb' l = 'aNPG!!u6sesA>[email protected](-' src = u + t + i + l # u 與 l 是固定字串,t是你要翻譯的字串,i是之前的時間戳 m2 = hashlib.md5() m2.update(src.encode("utf-8")) str_sent = m2.hexdigest() head = { 'Accept':'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding':'gzip, deflate', 'Accept-Language':'zh-CN,zh;q=0.9', 'Content-Length':'200', 'Connection':'keep-alive', 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', '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/63.0.3239.108 Safari/537.36', 'X-Requested-With':'XMLHttpRequest', } head['Cookie'] = '[email protected]; OUTFOX_SEARCH_USER_ID_NCOO=1846816080.1245883; ___rl__test__cookies='+str(time.time()*1000) data = { 'i': t, 'from':'AUTO', 'to':'AUTO', 'smartresult':'dict', 'client':'fanyideskweb', 'salt':i, #時間戳 'sign':str_sent, # 'doctype':'json', 'version':'2.1', 'keyfrom':'fanyi.web', 'action':'FY_BY_REALTIME', 'typoResult':'false' } s = requests.session() url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' p = s.post(url,data= data,headers = head) print(p.text)