有道翻译

关于请求

url

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

参数

i: hello
from: AUTO
to: AUTO
smartresult: dict
client: fanyideskweb
salt: 15772829507697
sign: f604f189cff72a34da1c49a844beed1a
ts: 1577282950769
bv: 42160534cfa82a6884077598362bbc9d
doctype: json
version: 2.1
keyfrom: fanyi.web
action: FY_BY_REALTlME

参数解析

i: h       # 待翻译字段
from: AUTO
to: AUTO
smartresult: dict
client: fanyideskweb
salt: 15772829507697    # 时间戳 + 一位随机数 (new Date).getTime()+parseInt(10 * Math.random(), 10)
sign: f604f189cff72a34da1c49a844beed1a    # 加密字段
ts: 1577282950769    # 时间戳 (new Date).getTime()
bv: 42160534cfa82a6884077598362bbc9d    # 加密字段 md5(navigator.appVersion) 
doctype: json
version: 2.1
keyfrom: fanyi.web        
action: FY_BY_REALTlME    # 触发方式(及时 或者 按钮)

salt的生成依赖于ts,注意时间戳的精确度
bv: JavaScript Window Navigator
sign: md5("fanyideskweb" + 待翻译文字 + salt + "n%A-rKaT5fb[Gy?;N5@Tj")

模拟请求

Content-Type:application/x-www-form-urlencoded
Referer:http://fanyi.youdao.com/
Cookie:OUTFOX_SEARCH_USER_ID_NCOO=1571439209.4880521; OUTFOX_SEARCH_USER_ID="1506839488@10.108.160.18"; ___rl__test__cookies=1577319471088

cookie中的___rl__test__cookies是个时间戳,OUTFOX_SEARCH_USER_ID中的@后面的是个ip, 前面的不知道是啥
好像这三个都可以随便写,测试的时候随便写出结果了

参考代码

凑活着看吧, 如果换ua的话, bv参数估计也要换一下

import random

import requests
import time
from hashlib import md5


def get_salt():
    return ts + str(random.randint(0, 9))


def get_sign(code):
    return md5(("fanyideskweb" + code + salt + "n%A-rKaT5fb[Gy?;N5@Tj").encode()).hexdigest()


def get_ts():
    return str(int(time.time() * 1000))


def get_bv():
    return md5(
        "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36".encode()).hexdigest()


def get_content(code):
    data = {
        "i": code,
        "from": "AUTO",
        "to": "AUTO",
        "smartresult": "dict",
        "client": "fanyideskweb",
        "salt": salt,
        "sign": get_sign(code),
        "ts": ts,
        "bv": get_bv(),
        "doctype": "json",
        "version": "2.1",
        "keyfrom": "fanyi.web",
        "action": "FY_BY_CLICKBUTTION"
    }

    res = requests.post(url="http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule",
                        data=data, headers=headers)
    print(res.json())


ts = get_ts()
salt = get_salt()

headers = {
    'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=34243243243; OUTFOX_SEARCH_USER_ID="2358766876@120.80.16.18"; ___rl__test__cookies=123213443242',

    'Referer': 'http://fanyi.youdao.com/',
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
}

get_content("hell")
{'translateResult': [[{'tgt': '地狱', 'src': 'hell'}]], 'errorCode': 0, 'type': 'en2zh-CHS', 'smartResult': {'entries': ['', 'n. 地狱;究竟(作加强语气词);训斥;黑暗势力\r\n', 'vi. 过放荡生活;飞驰\r\n', 'int. 该死;见鬼(表示惊奇、烦恼、厌恶、恼怒、失望等)\r\n'], 'type': 1}}

就先这样吧

posted @ 2019-12-26 10:08  寒菱  阅读(218)  评论(0编辑  收藏  举报