[Python] 纯文本查看 复制代码
import hashlib
import json
import random
import time
import requests
def youdao():
content = input("请输入您要翻译的内容:")
u = 'fanyideskweb'
d = content
# 生成salt
salt = str(int(time.time() * 1000) + random.randint(1, 10))
c = 'p09@Bn{h02_BIEe]$P^nG'
# 生成sign
sign = hashlib.md5((u + d + salt + c).encode('utf-8')).hexdigest()
url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
data = {
"i": content,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": salt,
"sign": sign,
# "ts": "1547536652328",
# "bv": "363eb5a1de8cfbadd0cd78bd6bd43bee",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTIME",
"typoResult": "false",
}
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Language": "zh-CN,zh;q = 0.9",
"Connection": "keep-alive",
"Content-Length": "271",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": "OUTFOX_SEARCH_USER_ID=-1105037136@10.168.8.64; JSESSIONID=aaadY5kjkBpKngxtXrqHw; OUTFOX_SEARCH_USER_ID_NCOO=1440145660.3035378; ___rl__test__cookies=1547536652325",
"Host": "fanyi.youdao.com",
"Origin": "http://fanyi.youdao.com",
"Referer": "http://fanyi.youdao.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
}
response = requests.post(url, data=data, headers=headers)
print(json.loads(response.content.decode()).get("translateResult")[0][0].get("tgt"))
print(response.content.decode())
if __name__ == '__main__':
youdao()