有道翻译接口获取
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有道翻译接口获取相关的知识,希望对你有一定的参考价值。
# 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#[email protected]" 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; [email protected]; 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])
以上是关于有道翻译接口获取的主要内容,如果未能解决你的问题,请参考以下文章