有道翻譯接口獲取

# coding:utf-8
import requests
import hashlib
import time
import json


def interpret(contents):
    url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'

    tt = int(time.time() * 1000)
    text = contents
    sig = "fanyideskweb" + text + str(tt) + "sr_3(QOHT)L2dx#uuGR@r"
    sign = hashlib.md5(sig.encode('utf-8')).hexdigest()

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

    headers = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Length": "223",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Cookie": "_ntes_nnid=4a572c8c5ac9526fdf4ba1240569aacd,1541596325684; OUTFOX_SEARCH_USER_ID_NCOO=1527476746.7479236; OUTFOX_SEARCH_USER_ID=-212498130@59.42.27.71; JSESSIONID=aaaMfSGQfy9VJhrvvGBCw; ___rl__test__cookies={}".format(
            tt),
        "Host": "fanyi.youdao.com",
        "Origin": "http://fanyi.youdao.com",
        "Pragma": "no-cache",
        "Referer": "http://fanyi.youdao.com/",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
    }

    response = requests.post(url, headers=headers, data=data)

    content = response.content.decode('utf-8')

    return json.loads(content)['translateResult'][0][0]['tgt']


if __name__ == '__main__':
    text = input('請你輸入翻譯內容:')

    print(interpret(text))


#
# ll ='[1,2,"ll"]' # {"name":"1235"}
# ss = str(ll)
# print(json.loads(ss)[0])
相關文章
相關標籤/搜索