翻译

from day1.fengzhuang_all import post,get
import time,random
import hashlib,json
from urllib import request

def md5_my(need_str):
# 创建对象
md5_o = hashlib.md5()
# 转换bytes
sign_bytes = need_str.encode(‘utf-8’)
# 更新md5_o的值
md5_o.update(sign_bytes)
# 函数内加密
sign_str = md5_o.hexdigest()
return sign_str

def fanyi(kw):

url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'

headers = {
    "Accept":"application/json, text/javascript, */*; q=0.01",
    # "Accept-Encoding":"gzip, deflate",
    "Accept-Language":"zh-CN,zh;q=0.9",
    "Connection":"keep-alive",
    # "Content-Length":"214",
    "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
    "Cookie":"OUTFOX_SEARCH_USER_ID=721443515@10.168.1.247; JSESSIONID=aaaEQkY8IkQPmhbI1A2uw; OUTFOX_SEARCH_USER_ID_NCOO=1926942658.7990084; ___rl__test__cookies=1534251665221",
    "Host":"fanyi.youdao.com",
    "Origin":"http://fanyi.youdao.com",
    "Referer":"http://fanyi.youdao.com/",
    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
    "X-Requested-With":"XMLHttpRequest",
}
n = kw
# 10 * Math.random(), 10     后面的10 是十进制
#  r = "" + ((new Date).getTime() + parseInt(10 * Math.random(), 10))
# 0-10之间整数
salt=int(time.time()*1000 + random.randint(0,10))
# print(salt)
# 整形转换成字符串
salt_str = str(salt)
# o = u.md5(S + n + r + D)
S = "fanyideskweb"
D = "ebSeFb%=XZ%T[KZ)c(sy!"
# 组合
sign_str = S + n + salt_str + D

# 加密
sign_md5_str = md5_my(sign_str)
# print(sign_md5_str)

data = {
    'i': n,
    'from': 'AUTO',
    'to': 'AUTO',
    'smartresult': 'dict',
    'client': 'fanyideskweb',
    'salt': salt_str,
    'sign': sign_md5_str,
    'doctype': 'json',
    'version': '2.1',
    'keyfrom': 'fanyi.web',
    'action': 'FY_BY_REALTIME',
    'typoResult': 'false',
}

html_bytes = post(url,data=data,headers=headers)
res_dict = json.loads(html_bytes.decode('utf-8'))
# print(html_bytes.decode('utf-8'))
translate = res_dict['translateResult'][0][0]['tgt']
return translate

if name == ‘main‘:

while True:
    a = str(input('请输入要翻译的词语:'))
    print(fanyi(a))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值