在Python中运行js报错,在js和Python中都打印过相关类型和值了,都没问题,这是怎么回事啊?
报错和异常为:
import requests
import execjs
import sys
import io
# 设置默认编码为utf-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
cookies = {
'sajssdk_2015_cross_new_user': '1',
'sensorsdata2015jssdkcross': '%7B%22distinct_id%22%3A%2219024c17c022c2-0219b491cdda7ee-4c657b58-1327104-19024c17c038c1%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkwMjRjMTdjMDIyYzItMDIxOWI0OTFjZGRhN2VlLTRjNjU3YjU4LTEzMjcxMDQtMTkwMjRjMTdjMDM4YzEifQ%3D%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%2C%22%24device_id%22%3A%2219024c17c022c2-0219b491cdda7ee-4c657b58-1327104-19024c17c038c1%22%7D',
}
headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'DNT': '1',
'Pragma': 'no-cache',
'Referer': 'https://www.swhysc.com/swhysc/financial/marginTradingList?channel=00010017000300020001&listId=2',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
params = {
'secuCode': '',
'market': '',
'orderFlag': '',
'pageNum': '1',
'pageSize': '10',
}
response = requests.get(
'https://www.swhysc.com/swhysc/interface/dsinfo/v1/margin/deposit/ratio',
params=params,
cookies=cookies,
headers=headers,
)
# 检查响应状态码
if response.status_code == 200:
try:
# 获取响应内容的UTF-8解码字符串
response_text = str(response.content.decode('utf-8'))
print(type(response_text))
decrypted_text = execjs.compile(open('./decrypt.js', 'r', encoding='utf-8').read()).call('decrypt',
response_text)
# 打印解密后的文本
print("Decrypted response:", decrypted_text)
except execjs.ProgramError as e:
print(f"JavaScript error: {e}")
except FileNotFoundError:
print("JavaScript file 'decrypt.js' not found.")
except UnicodeDecodeError as e:
print(f"Encoding error: {e}")
except ValueError as e:
print(f"Value error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
else:
print(f"Failed to fetch data: {response.status_code}")
// @charset "UTF-8";
const CryptoJs = require('crypto-js');
function decrypt(t) {
// return typeof t.replace(/\s+/g, '')
// t = t.replace(/\s+/g, '')
// 判断t是否为空
// if (t === '' || t === null || t === undefined) {
// return 't为空'
// }
var n = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : ""
, r = 'rewin-swhysc1234'
, i = CryptoJs.enc.Utf8.parse(r)
, o = CryptoJs.AES.decrypt(t, i, {
mode: CryptoJs.mode.ECB,
padding: CryptoJs.pad.Pkcs7
});
return CryptoJs.enc.Utf8.stringify(o).toString()
}
// // var ans = decrypt('I9Z12I0KnbyLBPtcgpYe7w48OHv1UCCmgTdiBdxFCN/2 YdTCn2m2UO3L20J8MUhRWkTnWM2t3JYft4y8sbT2uFfJGT/qAw3ufFlagtl3WrksWKyIrd/D68XVmUfKCUqIk3CdnI5jDk3iTh30nemjYbfjNJ18iIz/6 J7Ey/rjNOoeqO9L7P+4 IcOhGBps5oR0myWuXd66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gBIuNtJxlAY8Ec0h5nkp3DOPTSe5JO8QOPa0CDPzY/KYRVwptDi/CNU8prx0PmycbtW+LH6dU5vkOvofzkF23dfk82zbzHnLdPItGdsNw8HtshYeqNcBxdeY6C3qsRgIeRHp6h6QYC46kb54ryv5aoASLjbScZQGPBHNIeZ5Kdwzj00nuSTvEDj2tAgz82PymEVcKbQ4vwjVPKYcQuyFY8G7wTZxum9a2V1S8xxN7ueChtOgQTljc5MO3YfCmazrOsosvym7EEKyhm21LeSqEmvORLQhOA8UkJBQk3KCHdli/IrNTryK2S+gkLfjNJ18iIz/6 J7Ey/rjNOq2bwJ6/rhcT0Mi8B00PwT39ezVkikOyo9oNJ2D6rACsAeND2PPeK8B2oWjquKYLqpWh62/Vv+sunWhY9ufBzVSoYtnsxTK3xPuVtozXjSdncTOjc2anlMGX6chWnYmKfG8f3EphHBP2wk1Q/5 VonCD2Xmkd1xQpqYfh+P6lXeG9a1zvmKPXa5aSB //eVlLdtwQuiva3JxUfgjZg3OU9r4ddfWzJCC6ISelC8tVGz3cO28z8QMxCLnRh8k1GHEkv6MH0HdTpni3QaeEFuFfBaUUZ3Ay73q6jDolMiufrP7V/NGQtrR8+G0lge3XTyDhoXY='
// data = data.replace(/\s+/g, '')
// ans = decrypt(data)
// console.log(ans)
我首先对参数进行了确认,确认参数为正确格式,且值为正常的。
// return typeof t.replace(/\s+/g, '')
// t = t.replace(/\s+/g, '')
// 判断t是否为空
// if (t === '' || t === null || t === undefined) {
// return 't为空'
// }
其次关于编码问题
我在js顶端加了
// @charset "UTF-8";
在Python中用的
# 设置默认编码为utf-8
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
# 检查响应状态码
if response.status_code == 200:
try:
# 获取响应内容的UTF-8解码字符串
response_text = str(response.content.decode('utf-8'))
print(type(response_text))
decrypted_text = execjs.compile(open('./decrypt.js', 'r', encoding='utf-8').read()).call('decrypt',
response_text)
对应的Python代码
另外,一个小小的建议