2013-11-21 59 views
4

我一直在刮網站,它的工作原理,但突然它不再工作,我發現在他們的網站incapsula保護。我閱讀了新的服務條款,它提到只要它在人類內部可以做到限制,就可以從他們的網站獲得信息。但現在的問題是我的腳本遇到incapsula的JavaScript看起來像Python和Incapsula

var z="";var b="7472797B766172207868723B76617220743D6E6577204461746528292E67657454696D6528293B766172207374617475733D227374617274223B7661722074696D696E673D6E65772041727261792833293B77696E646F772E6F6E756E6C6F61643D66756E6374696F6E28297B74696D696E675B325D3D22723A222B286E6577204461746528292E67657454696D6528292D74293B646F63756D656E742E637265617465456C656D656E742822696D6722292E7372633D222F5F496E63617073756C615F5265736F757263653F4553324C555243543D363726743D373826643D222B656E636F6465555249436F6D706F6E656E74287374617475732B222028222B74696D696E672E6A6F696E28292B222922297D3B69662877696E646F772E584D4C4874747052657175657374297B7868723D6E657720584D4C48747470526571756573747D656C73657B7868723D6E657720416374697665584F626A65637428224D6963726F736F66742E584D4C4854545022297D7868722E6F6E726561647973746174656368616E67653D66756E6374696F6E28297B737769746368287868722E72656164795374617465297B6361736520303A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2072657175657374206E6F7420696E697469616C697A656420223B627265616B3B6361736520313A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2073657276657220636F6E6E656374696F6E2065737461626C6973686564223B627265616B3B6361736520323A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2072657175657374207265636569766564223B627265616B3B6361736520333A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2070726F63657373696E672072657175657374223B627265616B3B6361736520343A7374617475733D22636F6D706C657465223B74696D696E675B315D3D22633A222B286E6577204461746528292E67657454696D6528292D74293B6966287868722E7374617475733D3D323030297B706172656E742E6C6F636174696F6E2E72656C6F616428297D627265616B7D7D3B74696D696E675B305D3D22733A222B286E6577204461746528292E67657454696D6528292D74293B7868722E6F70656E2822474554222C222F5F496E63617073756C615F5265736F757263653F535748414E45444C3D323736303537313932393030303739323830342C31363537303433373735303338313336383533322C353931373335333737373039353633353432312C313735363339222C66616C7365293B7868722E73656E64286E756C6C297D63617463682863297B7374617475732B3D6E6577204461746528292E67657454696D6528292D742B2220696E6361705F6578633A20222B633B646F63756D656E742E637265617465456C656D656E742822696D6722292E7372633D222F5F496E63617073756C615F5265736F757263653F4553324C555243543D363726743D373826643D222B656E636F6465555249436F6D706F6E656E74287374617475732B222028222B74696D696E672E6A6F696E28292B222922297D3B";for (var i=0;i<b.length;i+=2){z=z+parseInt(b.substring(i, i+2), 16)+",";}z = z.substring(0,z.length-1); eval(eval('String.fromCharCode('+z+')')); 

,當我對其進行解碼,我發現然而

eval(
'try{var xhr;var t=new Date().getTime();var status="start";var timing=new Array(3);window.onunload=function(){timing[2]="r:"+(new Date().getTime()-t);myvar="/_Incapsula_Resource?ES2LURCT=67&t=78&d="+encodeURIComponent(status+" ("+timing.join()+")")};if(window.XMLHttpRequest){xhr=new XMLHttpRequest}else{xhr=new ActiveXObject("Microsoft.XMLHTTP")}xhr.onreadystatechange=function(){switch(xhr.readyState){case 0:status=new Date().getTime()-t+": request not initialized ";break;case 1:status=new Date().getTime()-t+": server connection established";break;case 2:status=new Date().getTime()-t+": request received";break;case 3:status=new Date().getTime()-t+": processing request";break;case 4:status="complete";timing[1]="c:"+(new Date().getTime()-t);if(xhr.status==200){parent.location.reload()}break}};timing[0]="s:"+(new Date().getTime()-t);xhr.open("GET","/_Incapsula_Resource?SWHANEDL=2760571929000792804,16570437750381368532,5917353777095635421,175639",false);xhr.send(null)}catch(c){status+=new Date().getTime()-t+" incap_exc: "+c;myvar="/_Incapsula_Resource?ES2LURCT=67&t=78&d="+encodeURIComponent(status+" ("+timing.join()+")")};' 
); 

評估使用pyV8

ReferenceError: ReferenceError: document is not defined ( @ 1 : 68) -> send(null)}catch(c){status+=new Date().getTime()-t+" incap_exc: "+c;document.c 
導致錯誤的語句

看來,PyV8不會創建文檔對象,所以它失敗了,當我將document.createElement(「img」)。src替換爲類似變量的東西時,變量將會有字符串

myvar = "/_Incapsula_Resource?ES2LURCT=67&t=78&d=start0%20incap_exc%3A%20ReferenceError%3A%20window%20is%20not%20defined%20(%2C%2C)" 

它似乎知道沒有窗口存在,我該如何刮這樣使用incapsula的網站?

回答

3

對此,我會建議Selenium。它可以讓你連接到瀏覽器並從中取出所有東西。瀏覽器負責渲染頁面,包括任何JavaScript,然後您可以訪問該DOM並提取所需內容。

http://docs.seleniumhq.org/

+0

硒與python集成嗎? – lostincomputer2

+0

確實如此。 https://pypi.python.org/pypi/selenium –