使用pyexecjs和nodejs抓取数据

给大家一个网址,大家可以试试去抓取这个网站的数据http://datamining.comratings.com/exam

 

打开后界面是这样的:

其实刚开始我也是有点懵逼的,因为这个相当于一个测试的网站,最简单逆向,看你会不会。

当你刷新之后就会出现你要的东西

然后就会打开新的窗口

 

获取里面的IP地址。 

刚开始也是过模拟浏览器之类的东西,但都不管用。

通过分析你会发现在你刷新的时候,网站会通过前台js向cookie里写入值,通过判断这个值,后台给你返回响应的数据

刚开始

 然后刷新:

基本就可以推测出刷新的时候加载的cookie。 

使用requests直接去请求,保存响应的内容,会发现里面只有一段js

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('l E(a){5 b=9.q;5 c=b.G("; ");M(5 i=0;i<c.D;i++){5 d=c[i].G("=");j(a==d[0]){g d[1]}}g""}m=E(\'m\');5 7="Q+/=";K=R.N;l r(a){5 b,i,k;5 c,e,n;k=a.D;i=0;b="";O(i<k){c=a.o(i++)&P;j(i==k){b+=7.8(c>>2);b+=7.8((c&p)<<4);b+="==";s}e=a.o(i++);j(i==k){b+=7.8(c>>2);b+=7.8(((c&p)<<4)|((e&A)>>4));b+=7.8((e&B)<<2);b+="=";s}n=a.o(i++);b+=7.8(c>>2);b+=7.8(((c&p)<<4)|((e&A)>>4));b+=7.8(((e&B)<<2)|((n&S)>>6));b+=7.8(n&L)}g b}l H(){5 w=f.10||9.v.z||9.u.z;5 h=f.11||9.v.C||9.u.C;j(w*h<=12){g I}5 x=f.13;5 y=f.Z;j(x+w<=0||y+h<=0||x>=f.t.W||y>=f.t.V){g I}g X}l J(){j(H()){}Y{5 a="";a="T="+r(m.U(1,3))+"; F=/";9.q=a;a="e="+r(m)+"; F=/";9.q=a;f.14(K)}}J();',62,67,'|||||var||encoderchars|charAt|document|||||c2|window|return|||if|len|function|session|c3|charCodeAt|0x3|cookie|f1|break|screen|body|documentElement||||clientWidth|0xf0|0xf|clientHeight|length|getCookie|path|split|findDimensions|true|reload|url|0x3f|for|href|while|0xff|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789|location|0xc0|c1|substr|height|width|false|else|screenY|innerWidth|innerHeight|120000|screenX|open'.split('|'),0,{}))

就是这么一段js.怎么去执行这个js呢,就是安装pyexecjs,当然Python执行js的库,不止这一个。

pip install PyExecJS

代码:

import execjs


str_content = execjs.eval(open(r"demo.js", encoding='utf8').read())
print(str_content)

执行完成以后就会有下面的结果:

function getCookie(a){var b=document.cookie;var c=b.split("; ");for(var i=0;i<c.length;i++){var d=c[i].split("=");if(a==d[0]){return d[1]}}return""}session=getCookie('session');var encoderchars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";url=location.href;function f1(a){var b,i,len;var c,c2,c3;len=a.length;i=0;b="";while(i<len){c=a.charCodeAt(i++)&0xff;if(i==len){b+=encoderchars.charAt(c>>2);b+=encoderchars.charAt((c&0x3)<<4);b+="==";break}c2=a.charCodeAt(i++);if(i==len){b+=encoderchars.charAt(c>>2);b+=encoderchars.charAt(((c&0x3)<<4)|((c2&0xf0)>>4));b+=encoderchars.charAt((c2&0xf)<<2);b+="=";break}c3=a.charCodeAt(i++);b+=encoderchars.charAt(c>>2);b+=encoderchars.charAt(((c&0x3)<<4)|((c2&0xf0)>>4));b+=encoderchars.charAt(((c2&0xf)<<2)|((c3&0xc0)>>6));b+=encoderchars.charAt(c3&0x3f)}return b}function findDimensions(){var w=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth;var h=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;if(w*h<=120000){return true}var x=window.screenX;var y=window.screenY;if(x+w<=0||y+h<=0||x>=window.screen.width||y>=window.screen.height){return true}return false}function reload(){if(findDimensions()){}else{var a="";a="c1="+f1(session.substr(1,3))+"; path=/";document.cookie=a;a="c2="+f1(session)+"; path=/";document.cookie=a;window.open(url)}}reload();

分析这个js会发现这个就是生成cookie的js,怎么获取重新生成的cookie,当然还是继续执行js了

这个时候就需要安装jsdom库了,这个是nodejs的库

因为这段js中有document,需要生成一个document,window

npm install jsdom

执行之前先换到国内源,速度快,

npm config set registry https://registry.npm.taobao.org

在执行上面的命令就行,当然前提是安装了nodejs,并且需要在你项目所在的目录执行这个命令,如果不在这个项目目录下,就会出现检测不到jsdom库的情况。

安装过程中可能 一些错误,参考博客https://blog.csdn.net/genius_yym/article/details/84645915,基本是能解决的

放代码了:

// function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1;};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p;}('l E(a){5 b=9.q;5 c=b.G("; ");M(5 i=0;i<c.D;i++){5 d=c[i].G("=");j(a==d[0]){g d[1]}}g""}m=E(\'m\');5 7="Q+/=";K=R.N;l r(a){5 b,i,k;5 c,e,n;k=a.D;i=0;b="";O(i<k){c=a.o(i++)&P;j(i==k){b+=7.8(c>>2);b+=7.8((c&p)<<4);b+="==";s}e=a.o(i++);j(i==k){b+=7.8(c>>2);b+=7.8(((c&p)<<4)|((e&A)>>4));b+=7.8((e&B)<<2);b+="=";s}n=a.o(i++);b+=7.8(c>>2);b+=7.8(((c&p)<<4)|((e&A)>>4));b+=7.8(((e&B)<<2)|((n&S)>>6));b+=7.8(n&L)}g b}l H(){5 w=f.10||9.v.z||9.u.z;5 h=f.11||9.v.C||9.u.C;j(w*h<=12){g I}5 x=f.13;5 y=f.Z;j(x+w<=0||y+h<=0||x>=f.t.W||y>=f.t.V){g I}g X}l J(){j(H()){}Y{5 a="";a="T="+r(m.U(1,3))+"; F=/";9.q=a;a="e="+r(m)+"; F=/";9.q=a;f.14(K)}}J();',62,67,'|||||var||encoderchars|charAt|document|||||c2|window|return|||if|len|function|session|c3|charCodeAt|0x3|cookie|f1|break|screen|body|documentElement||||clientWidth|0xf0|0xf|clientHeight|length|getCookie|path|split|findDimensions|true|reload|url|0x3f|for|href|while|0xff|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789|location|0xc0|c1|substr|height|width|false|else|screenY|innerWidth|innerHeight|120000|screenX|open'.split('|'),0,{})
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM('<html><head></head><body>开启js,刷新吧,\n' +
    '<script>\n' +
    '    eval(function (p, a, c, k, e, d) {\n' +
    '        e = function (c) {\n' +
    '            return (c < a ? "" : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36))\n' +
    '        };\n' +
    '        if (!\'\'.replace(/^/, String)) {\n' +
    '            while (c--) d[e(c)] = k[c] || e(c);\n' +
    '            k = [function (e) {\n' +
    '                return d[e]\n' +
    '            }];\n' +
    '            e = function () {\n' +
    '                return \'\\\\w+\'\n' +
    '            };\n' +
    '            c = 1;\n' +
    '        }\n' +
    '        ;\n' +
    '        while (c--) if (k[c]) p = p.replace(new RegExp(\'\\\\b\' + e(c) + \'\\\\b\', \'g\'), k[c]);\n' +
    '        return p;\n' +
    '    }(\'l E(a){5 b=9.q;5 c=b.G("; ");M(5 i=0;i<c.D;i++){5 d=c[i].G("=");j(a==d[0]){g d[1]}}g""}m=E(\\\'m\\\');5 7="Q+/=";K=R.N;l r(a){5 b,i,k;5 c,e,n;k=a.D;i=0;b="";O(i<k){c=a.o(i++)&P;j(i==k){b+=7.8(c>>2);b+=7.8((c&p)<<4);b+="==";s}e=a.o(i++);j(i==k){b+=7.8(c>>2);b+=7.8(((c&p)<<4)|((e&A)>>4));b+=7.8((e&B)<<2);b+="=";s}n=a.o(i++);b+=7.8(c>>2);b+=7.8(((c&p)<<4)|((e&A)>>4));b+=7.8(((e&B)<<2)|((n&S)>>6));b+=7.8(n&L)}g b}l H(){5 w=f.10||9.v.z||9.u.z;5 h=f.11||9.v.C||9.u.C;j(w*h<=12){g I}5 x=f.13;5 y=f.Z;j(x+w<=0||y+h<=0||x>=f.t.W||y>=f.t.V){g I}g X}l J(){j(H()){}Y{5 a="";a="T="+r(m.U(1,3))+"; F=/";9.q=a;a="e="+r(m)+"; F=/";9.q=a;f.14(K)}}J();\', 62, 67, \'|||||var||encoderchars|charAt|document|||||c2|window|return|||if|len|function|session|c3|charCodeAt|0x3|cookie|f1|break|screen|body|documentElement||||clientWidth|0xf0|0xf|clientHeight|length|getCookie|path|split|findDimensions|true|reload|url|0x3f|for|href|while|0xff|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789|location|0xc0|c1|substr|height|width|false|else|screenY|innerWidth|innerHeight|120000|screenX|open\'.split(\'|\'), 0, {}))\n' +
    '</script>\n' +
    '</body></html>');
window = dom.window;
document = window.document;
// function getCookie(a)
// {
//     var b= "session=0b45495fefe1528103982a7f043f10be;";
//     var c=b.split("; ");
//     for(var i=0;i<c.length;i++) {
//         var d=c[i].split("=");
//         if(a==d[0]){
//             return d[1]
//         }
//     }
//     return""
// }
// session=getCookie('session');
var encoderchars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
url='http://datamining.comratings.com/exam';
function f1(a){
    var b,i,len;
    var c,c2,c3;
    len=a.length;
    i=0;b="";
    while(i<len){
        c=a.charCodeAt(i++)&0xff;
        if(i==len){
            b+=encoderchars.charAt(c>>2);
            b+=encoderchars.charAt((c&0x3)<<4);
            b+="==";break
        }
        c2=a.charCodeAt(i++);
        if(i==len){
            b+=encoderchars.charAt(c>>2);
            b+=encoderchars.charAt(((c&0x3)<<4)|((c2&0xf0)>>4));
            b+=encoderchars.charAt((c2&0xf)<<2);
            b+="=";break
        }
        c3=a.charCodeAt(i++);
        b+=encoderchars.charAt(c>>2);
        b+=encoderchars.charAt(((c&0x3)<<4)|((c2&0xf0)>>4));
        b+=encoderchars.charAt(((c2&0xf)<<2)|((c3&0xc0)>>6));
        b+=encoderchars.charAt(c3&0x3f)
    }
    return b
}
function findDimensions(){
    var w=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth;
    var h=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;
    if(w*h<=120000){
        return true
    }
    var x=window.screenX;
    var y=window.screenY;
    if(x+w<=0||y+h<=0||x>=window.screen.width||y>=window.screen.height){
        return true
    }
    return false
}
function reload(session){

    var a="";
    a1="c1="+f1(session.substr(1,3))+"; c2=" + f1(session) + ";";
    // c1 = a1
    // // document.cookie=a;
    // a2="c2="+f1(session)+"; path=/";
    // c2 = a2
    // document.cookie=a2;
    return a1
    // window.open(url);

    // return 1
}
// reload();

这个是已经被我处理过的js,可以自己试试。

 

import requests
# import js2py
import execjs



# print(str_content)

res = requests.get('http://datamining.comratings.com/exam')
cookie = res.cookies
print(cookie.values(), type(cookie.values()))
str_content = execjs.compile(open(r"demo.js", encoding='utf8').read()).call('reload', cookie.values()[0])
# str_content = execjs.eval(open(r"demo.js", encoding='utf8').read())
print(str_content)

name1 = str_content.split(';')[0].split('=')[0]
value1 = str_content.split(';')[0].split('=')[1]
value2 = str_content.split(';')[1].split('c2=')[1]
# name2 = str_content.split(';')[1].split('=')[0]
cookie.set(name1, value1)
# res2 = requests.get('http://datamining.comratings.com/exam', cookies=cookie)
cookie.set('c2', value2)
res3 = requests.get('http://datamining.comratings.com/exam3', cookies=cookie)
with open('content_exam.html', 'r', encoding='utf8') as f:
    f.write(res3.text)

这样就能拿到具体内容了

不过这些内容还需要更进一步的清洗,因为有些是没有的用,如display:none

到这就完成了

参考文章:https://blog.csdn.net/hpulfc/article/details/80084398

欢迎关注我的微信公众号,有更多关于爬虫,机器学习的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裸睡的雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值