# -*- coding: utf-8 -*- """ Created on 2023-09-12 --------- @summary: cookies -> security_session_mid_verify --------- @author: Lzz """ import time import requests import execjs def yun_suo_auto_jump_by_js(url): ctx = execjs.compile(''' function YunSuoAutoJump(url) { function stringToHex(str) { var val = ""; for (var i = 0; i < str.length; i++) { if (val == "") val = str.charCodeAt(i).toString(16); else val += str.charCodeAt(i).toString(16); } return val; } var width = 1536; var height = 864; var screendate = width + "," + height; location = url + "?security_verify_data=" + stringToHex(screendate); return location } ''') return ctx.call("YunSuoAutoJump", url) def string_to_hex(s): # 将字符串中的每个字符转换为十六进制,不添加0x前缀 return ''.join([format(ord(char), 'x') for char in s]) def yun_suo_auto_jump_by_py(url): width = 1536 height = 864 screen_data = f"{width},{height}" # 生成包含安全验证数据的URL return f"{url}?security_verify_data={string_to_hex(screen_data)}" def yun_suo_auto_jump(url, platform='py'): if platform == 'js': return yun_suo_auto_jump_by_js(url) else: return yun_suo_auto_jump_by_py(url) def get_mid_code(url, proxies=None, platform=None): headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Accept-Language": "zh-CN,zh;q=0.9", "Cache-Control": "no-cache", "Connection": "keep-alive", "Pragma": "no-cache", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" } with requests.session() as session: session.proxies = proxies request_params = dict(headers=headers, timeout=60, verify=False) session.get(url, **request_params) yz_url = yun_suo_auto_jump(url, platform=platform) count, cookies = 0, {} while count < 10: session.get(yz_url, headers=headers, timeout=60, verify=False) cookies = session.cookies.get_dict() if cookies.get('security_session_mid_verify'): break count += 1 time.sleep(2) return cookies