使用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

歡迎關注我的微信公衆號,有更多關於爬蟲,機器學習的內容

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章