# -*- coding: utf-8 -*-
import requests
import json
import time
import random
import hashlib
url='https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'

headers={
        #1.ua,反爬验证1
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',
        #2.来源
        'Referer':'http://fanyi.youdao.com/',
        'Cookie':'OUTFOX_SEARCH_USER_ID=-522843246@10.108.160.18; JSESSIONID=aaaFzjvImjFwabRqK39mx; OUTFOX_SEARCH_USER_ID_NCOO=1524476628.2190244; ___rl__test__cookies=1594470854108'
        }
data_old={#从js中找
'i':'吉',
'from':'AUTO',
'to':'AUTO',
'smartresult':'dict',
'client':'fanyideskweb',
'salt':'15944708541155',
'sign':'67257b4c0bd3728a5770c7a6469e4466',
'ts':'1594470854115',
'bv':'0e9e49a54501ac521c87c21e9c041e0b',
'doctype':'json',
'version':'2.1',
'keyfrom':'fanyi.web',
'action':'FY_BY_REALTlME'
}
r=str(int(time.time()*1000))
random_num=random.randint(0,9)
i=r+str(random_num)

def data_new(e):
    sign="fanyideskweb"+e+i+"mmbP%A-r6U3Nw(n]BjuEU"
    md5=hashlib.md5()
    md5.update(sign.encode())
    signNew=md5.hexdigest()
    data={
    'i':e,
    'from':'AUTO',
    'to':'AUTO',
    'smartresult':'dict',
    'client':'fanyideskweb',
    'salt':i,
    'sign':signNew,
    'ts':r,
    'bv':'0e9e49a54501ac521c87c21e9c041e0b',
    'doctype':'json',
    'version':'2.1',
    'keyfrom':'fanyi.web',
    'action':'FY_BY_REALTlME'
    }
    return data
data=data_new('上床')
#r=requests.post(url,data=data_old,headers=headers).text
r=requests.post(url,data=data,headers=headers).text
r_dict=json.loads(r)
print(r_dict['translateResult'][0][0]['tgt'])