返回类似JSON的网站,但我无法在Python中解析它

9bfwbjaz  于 2023-03-24  发布在  Python
关注(0)|答案(1)|浏览(193)

我试图从网站“https://bet.hkjc.com/marksix/getJSON.aspx?sd=20230101&ed=20230331&sb=0”读取JSON文件。
当我使用浏览器打开网站时,将网站中显示的文本复制并粘贴到JSON查看器中,它确实显示了一个有效的结构。
你知道为什么它不能与通常的方法requests.get('url').json()一起工作吗?
我想要一个工作代码,可以读取网站和存储到一个变量使用Python的JSON。

gopyfrb3

gopyfrb31#

网站返回一个程序,然后请求JSON

response = requests.get("https://bet.hkjc.com/marksix/getJSON.aspx?sd=20230101&ed=20230331&sb=0")

答案是这样的:

<HTML>
<head>
<script>
Challenge=485021;
ChallengeId=1251691391;
GenericErrorMessageCookies="Cookies must be enabled in order to view this page.";
</script>
<script>
function test(var1)
{
        var var_str=""+Challenge;
        var var_arr=var_str.split("");
        var LastDig=var_arr.reverse()[0];
        var minDig=var_arr.sort()[0];
        var subvar1 = (2 * (var_arr[2]))+(var_arr[1]*1);
        var subvar2 = (2 * var_arr[2])+var_arr[1];
        var my_pow=Math.pow(((var_arr[0]*1)+2),var_arr[1]);
        var x=(var1*3+subvar1)*1;
        var y=Math.cos(Math.PI*subvar2);
        var answer=x*y;
        answer-=my_pow*1;
        answer+=(minDig*1)-(LastDig*1);
        answer=answer+subvar2;
        return answer;
}
</script>
<script>
client = null;
if (window.XMLHttpRequest)
{
        var client=new XMLHttpRequest();
}
else
{
        if (window.ActiveXObject)
        {
                client = new ActiveXObject('MSXML2.XMLHTTP.3.0');
        };
}
if (!((!!client)&&(!!Math.pow)&&(!!Math.cos)&&(!![].sort)&&(!![].reverse)))
{
        document.write("Not all needed JavaScript methods are supported.<BR>");

}
else
{
        client.onreadystatechange  = function()
        {
                if(client.readyState  == 4)
                {
                        var MyCookie=client.getResponseHeader("X-AA-Cookie-Value");
                        {
                                document.write(client.responseText);
                                return;
                        }

                        var cookieName = MyCookie.split('=')[0];
                        if (document.cookie.indexOf(cookieName)==-1)
                        {
                                document.write(GenericErrorMessageCookies);
                                return;
                        }
                        window.location.reload(true);
                }
        };
        y=test(Challenge);
        client.open("POST",window.location,true);
        client.setRequestHeader('X-AA-Challenge-ID', ChallengeId);
        client.setRequestHeader('X-AA-Challenge-Result',y);
        client.setRequestHeader('X-AA-Challenge',Challenge);
        client.setRequestHeader('Content-Type' , 'text/plain');
        client.send();
}
</script>
</head>
<body>
<noscript>JavaScript must be enabled in order to view this page.</noscript>
</body>
</HTML>

这是发送请求的关键部分:

y=test(Challenge);
        client.open("POST",window.location,true);
        client.setRequestHeader('X-AA-Challenge-ID', ChallengeId);
        client.setRequestHeader('X-AA-Challenge-Result',y);
        client.setRequestHeader('X-AA-Challenge',Challenge);
        client.setRequestHeader('Content-Type' , 'text/plain');
        client.send();
  • That* request返回JSON。

相关问题