import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs
import requests

def medthod_execjs(js_path,function_name,*args):
js_code = open(js_path, encoding='utf-8').read()
ctx = execjs.compile(js_code)

# 第一个参数为ja代码中的函数名, 后面为函数对应的参数
result = ctx.call(function_name,*args)
return result
# print(result)


def js_decrpt(data):
sgin = medthod_execjs('md5案例_有道翻译.js', 'f', data)
return sgin

def main(data):
headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Origin": "https://youdao.com",
"Referer": "https://youdao.com/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
"sec-ch-ua": "\"Not)A;Brand\";v=\"99\", \"Google Chrome\";v=\"127\", \"Chromium\";v=\"127\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\""
}
# cookies = {
# "OUTFOX_SEARCH_USER_ID": "-1200019761@183.19.229.110",
# "OUTFOX_SEARCH_USER_ID_NCOO": "1019806497.100566",
# "_ga": "GA1.2.81133647.1719495347",
# "_ga_ZSH399DT89": "GS1.2.1719495347.1.0.1719495351.56.0.0",
# "DICT_DOCTRANS_SESSION_ID": "ZjM1MDVmYWUtZjUxYi00ZTgxLTgxYzktYjkwOWU4NWRiNzkz",
# "_uetsid": "e84602a05c6911efb3a1a32c9317ac55",
# "_uetvid": "e8460d205c6911efb49b017b0918922e"
# }
url = "https://dict.youdao.com/jsonapi_s"
params = {
"doctype": "json",
"jsonversion": "4"
}
data = {
"q": data,
"le": "en",
"sign": js_decrpt(data),
"keyfrom": "webdict"
}
# response = requests.post(url, headers=headers, cookies=cookies, params=params, data=data)
response = requests.post(url, headers=headers, params=params, data=data)

print(response.json())
print(response)
if __name__ == '__main__':
#翻译price单词
word = 'price'
main(word)

posted on 2024-09-06 16:13  我爱你的  阅读(7)  评论(0编辑  收藏  举报