2016-12-09 65 views
0

我試圖接收一個簡單的GET請求,但我從我期望收到不同的迴應。 使用下面的代碼:Python請求|錯誤的GET迴應

import requests 
url = 'http://ropinwev.pinbet88.com/sports-service/sv/odds/events' 
params = { 
      "mk" : 1, 
      "sp" : 29, 
      "ot" : 0, 
      "btg" : 1, 
      "o" : 1, 
      "lg" : "", 
      "ev" : "", 
      "d" : "2016-12-10", 
      "l" : 3, 
      "v" : 0, 
      "more" : "false", 
      "c" : "IL", 
      "tm" : 0, 
      "g" : "", 
      "pa" : 0, 
      "_" : 1481265021488, 
      "locale" : 'he_IL' 
      } 
resp = requests.get(url, params = params).text 

輸出是:

<html> 
<head> 
<META NAME="robots" CONTENT="noindex,nofollow"> 
<script> 
(function(){function getSessionCookies(){var cookieArray=new Array();var cName=/^\s?incap_ses_/;var c=document.cookie.split(";");for(var i=0;i<c.length;i++){var key=c[i].substr(0,c[i].indexOf("="));var value=c[i].substr(c[i].indexOf("=")+1,c[i].length);if(cName.test(key)){cookieArray[cookieArray.length]=value}}return cookieArray}function setIncapCookie(vArray){var res;try{var cookies=getSessionCookies();var digests=new Array(cookies.length);for(var i=0;i<cookies.length;i++){digests[i]=simpleDigest((vArray)+cookies[i])}res=vArray+",digest="+(digests.join())}catch(e){res=vArray+",digest="+(encodeURIComponent(e.toString()))}createCookie("___utmvc",res,20)}function simpleDigest(mystr){var res=0;for(var i=0;i<mystr.length;i++){res+=mystr.charCodeAt(i)}return res}function createCookie(name,value,seconds){var expires="";if(seconds){var date=new Date();date.setTime(date.getTime()+(seconds*1000));var expires="; expires="+date.toGMTString()}document.cookie=name+"="+value+expires+"; path=/"}function test(o){var res="";var vArray=new Array();for(var j=0;j<o.length;j++){var test=o[j][0];switch(o[j][1]){case"exists":try{if(typeof(eval(test))!="undefined"){vArray[vArray.length]=encodeURIComponent(test+"=true")}else{vArray[vArray.length]=encodeURIComponent(test+"=false")}}catch(e){vArray[vArray.length]=encodeURIComponent(test+"=false")}break;case"value":try{try{res=eval(test);if(typeof(res)==="undefined"){vArray[vArray.length]=encodeURIComponent(test+"=undefined")}else if(res===null){vArray[vArray.length]=encodeURIComponent(test+"=null")}else{vArray[vArray.length]=encodeURIComponent(test+"="+res.toString())}}catch(e){vArray[vArray.length]=encodeURIComponent(test+"=cannot evaluate");break}break}catch(e){vArray[vArray.length]=encodeURIComponent(test+"="+e)}case"plugin_extentions":try{var extentions=[];try{i=extentions.indexOf("i")}catch(e){vArray[vArray.length]=encodeURIComponent("plugin_ext=indexOf is not a function");break}try{var num=navigator.plugins.length if(num==0||num==null){vArray[vArray.length]=encodeURIComponent("plugin_ext=no plugins");break}}catch(e){vArray[vArray.length]=encodeURIComponent("plugin_ext=cannot evaluate");break}for(var i=0;i<navigator.plugins.length;i++){if(typeof(navigator.plugins[i])=="undefined"){vArray[vArray.length]=encodeURIComponent("plugin_ext=plugins[i] is undefined");break}var filename=navigator.plugins[i].filename var ext="no extention";if(typeof(filename)=="undefined"){ext="filename is undefined"}else if(filename.split(".").length>1){ext=filename.split('.').pop()}if(extentions.indexOf(ext)<0){extentions.push(ext)}}for(i=0;i<extentions.length;i++){vArray[vArray.length]=encodeURIComponent("plugin_ext="+extentions[i])}}catch(e){vArray[vArray.length]=encodeURIComponent("plugin_ext="+e)}break}}vArray=vArray.join();return vArray}var o=[["navigator","exists"],["navigator.vendor","value"],["navigator.appName","value"],["navigator.plugins.length==0","value"],["navigator.platform","value"],["navigator.webdriver","value"],["platform","plugin_extentions"],["ActiveXObject","exists"],["webkitURL","exists"],["_phantom","exists"],["callPhantom","exists"],["chrome","exists"],["yandex","exists"],["opera","exists"],["opr","exists"],["safari","exists"],["awesomium","exists"],["puffinDevice","exists"],["navigator.cpuClass","exists"],["navigator.oscpu","exists"],["navigator.connection","exists"],["window.outerWidth==0","value"],["window.outerHeight==0","value"],["window.WebGLRenderingContext","exists"],["document.documentMode","value"],["eval.toString().length","value"]];try{setIncapCookie(test(o));document.createElement("img").src="/_Incapsula_Resource?SWKMTFSR=1&e="+Math.random()}catch(e){img=document.createElement("img");img.src="/_Incapsula_Resource?SWKMTFSR=1&e="+e}})(); 
</script> 
<script> 
(function() { 
var z="";var b="7472797B766172207868723B76617220743D6E6577204461746528292E67657454696D6528293B766172207374617475733D227374617274223B7661722074696D696E673D6E65772041727261792833293B77696E646F772E6F6E756E6C6F61643D66756E6374696F6E28297B74696D696E675B325D3D22723A222B286E6577204461746528292E67657454696D6528292D74293B646F63756D656E742E637265617465456C656D656E742822696D6722292E7372633D222F5F496E63617073756C615F5265736F757263653F4553324C555243543D363726743D373826643D222B656E636F6465555249436F6D706F6E656E74287374617475732B222028222B74696D696E672E6A6F696E28292B222922297D3B69662877696E646F772E584D4C4874747052657175657374297B7868723D6E657720584D4C48747470526571756573747D656C73657B7868723D6E657720416374697665584F626A65637428224D6963726F736F66742E584D4C4854545022297D7868722E6F6E726561647973746174656368616E67653D66756E6374696F6E28297B737769746368287868722E72656164795374617465297B6361736520303A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2072657175657374206E6F7420696E697469616C697A656420223B627265616B3B6361736520313A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2073657276657220636F6E6E656374696F6E2065737461626C6973686564223B627265616B3B6361736520323A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2072657175657374207265636569766564223B627265616B3B6361736520333A7374617475733D6E6577204461746528292E67657454696D6528292D742B223A2070726F63657373696E672072657175657374223B627265616B3B6361736520343A7374617475733D22636F6D706C657465223B74696D696E675B315D3D22633A222B286E6577204461746528292E67657454696D6528292D74293B6966287868722E7374617475733D3D323030297B706172656E742E6C6F636174696F6E2E72656C6F616428297D627265616B7D7D3B74696D696E675B305D3D22733A222B286E6577204461746528292E67657454696D6528292D74293B7868722E6F70656E2822474554222C222F5F496E63617073756C615F5265736F757263653F535748414E45444C3D333439373530313934313631363137343235312C393139363137343737343436373131303233382C363538393731353830363932353239363130332C31343131393334222C66616C7365293B7868722E73656E64286E756C6C297D63617463682863297B7374617475732B3D6E6577204461746528292E67657454696D6528292D742B2220696E6361705F6578633A20222B633B646F63756D656E742E637265617465456C656D656E742822696D6722292E7372633D222F5F496E63617073756C615F5265736F757263653F4553324C555243543D363726743D373826643D222B656E636F6465555249436F6D706F6E656E74287374617475732B222028222B74696D696E672E6A6F696E28292B222922297D3B";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+')'));})(); 
</script></head> 
<body> 
<iframe style="display:none;visibility:hidden;" src="//content.incapsula.com/jsTest.html" id="gaIframe"></iframe> 
</body></html> 

但我預計接收:

{"u":null,"l":[[29,"Soccer",[],1481249219059,0,null,[0,0,0],0]],"n":[[29,"Soccer",[[1766,"Australia - A League",[[670363474,"Sydney FC (n)","Melbourne City",5,1481273400000,0,1,5,{"0":[[[0.25,-0.25,"0-0.5","-105","-103",1,0,356978225,0],[0.5,-0.5,"0.5","126","-140",1,0,1318088997,1],[0.0,0.0,"0.0","-163","144",0,0,1318088999,1]],[["2.5",2.5,"-101","-110",356978225,0],["2.0",2.0,"-212","175",1318088996,1],["2-2.5",2.25,"-136","120",1318088998,1]],["243","126","245",356978225,0],0,null,1,0],"1":[[[0.25,-0.25,"0-0.5","133","-152",1,0,356978227,0]],[["1.0",1.0,"-102","-111",356978227,0]],null,1,null,1,0]},null,null,[0,1],0,null,null,null,null,"I",1,0,0,14]],null],[2118,"Iran - Pro League",[[671792002,"Padideh Khorasan","Persepolis Tehran",5,1481283000000,0,0,2,{"0":[[[-0.5,0.5,"0.5","-122","102",0,1,356978905,0]],[["2.0",2.0,"-103","-115",356978905,0],["1.5",1.5,"-197","144",1318096696,1],["1.5-2",1.75,"-148","116",1318096698,1]],null,0,null,0,0]},null,null,[0,1],0,null,null,null,null,"I",0,0,0,6],[671248014,"Sepahan Esfahan","Saba Qom",5,1481283900000,0,1,2,{"0":[[[0.5,-0.5,"0.5","-111","-106",1,0,356978857,0]],[["2.0",2.0,"-114","-104",356978857,0],["1.5",1.5,"-222","161",1318096148,1],["1.5-2",1.75,"-165","129",1318096150,1]],null,0,null,1,0]},null,null,[0,1],0,null,null,null,null,"I",0,0,0,6],[671248035,"Esteghlal Khuzestan","Foolad Khuzestan",5,1481292600000,0,1,2,{"0":[[[0.25,-0.25,"0-0.5","104","-125",1,0,356978977,0]],[["2-2.5",2.25,"116","-142",356978977,0],["1.5-2",1.75,"-172","130",1318097420,1],["2.0",2.0,"-124","-100",1318097422,1]],null,0,null,1,0]},null,null,[0,1],0,null,null,null,null,"I",0,0,0,6]],null],[2395,"Romania - Liga 1",[[670897767,"ACS Poli Timisoara","SC FC Voluntari SA",5,1481295600000,0,1,5,{"0":[[[0.0,0.0,"0.0","-111","-100",0,0,356943032,0],[0.25,-0.25,"0-0.5","129","-148",1,0,1317648045,1],[-0.25,0.25,"0-0.5","-164","141",0,1,1317648047,1]],[["2-2.5",2.25,"-123","107",356943032,0],["1.5-2",1.75,"-252","194",1317648044,1],["2.0",2.0,"-188","154",1317648046,1]],["191","178","210",356943032,0],0,null,0,0],"1":[[[0.0,0.0,"0.0","-107","-105",0,0,356943033,0]],[["0.5-1",0.75,"-147","126",356943033,0]],null,1,null,0,0]},null,null,[0,1],0,null,null,null,null,"I",1,0,0,14],[670901531,"CS Universitatea Craiova","CFR Cluj",5,1481308200000,0,1,5,{"0":[[[0.25,-0.25,"0-0.5","-110","-101",1,0,356978942,0],[0.5,-0.5,"0.5","123","-142",1,0,1318097071,1],[0.0,0.0,"0.0","-178","152",0,0,1318097073,1]],[["2-2.5",2.25,"104","-119",356978942,0],["1.5-2",1.75,"-190","155",1318097070,1],["2.0",2.0,"-138","118",1318097072,1]],["276","126","216",356978942,0],0,null,1,0],"1":[[[0.0,0.0,"0.0","-140","121",0,0,356978944,0]],[["0.5-1",0.75,"-127","111",356978944,0]],null,1,null,0,0]},null,null,[0,1],0,null,null,null,null,"I",1,0,0,14]],null],[2578,"Turkey - 1st League",[[671605424,"Gaziantep Buyuksehir Belediyespor","Elazigspor",5,1481297400000,0,1,5,{"0":[[[0.5,-0.5,"0.5","-118","103",1,0,356978861,0],[0.75,-0.75,"0.5-1","113","-131",1,0,1318096173,1],[0.25,-0.25,"0-0.5","-174","144",1,0,1318096175,1]],[["2-2.5",2.25,"104","-119",356978861,0],["1.5-2",1.75,"-190","155",1318096172,1],["2.0",2.0,"-138","118",1318096174,1]],["382","-118","249",356978861,0],0,null,1,0],"1":[[[0.25,-0.25,"0-0.5","101","-115",1,0,356978863,0]],[["0.5-1",0.75,"-134","115",356978863,0]],null,1,null,1,0]},null,null,[0,1],0,null,null,null,null,"I",1,0,0,14]],null],[1880,"Croatia - 1. HNL",[[670981335,"Slaven Koprivnica","Inter Zapresic",5,1481302800000,0,1,5,{"0":[[[0.5,-0.5,"0.5","-123","107",1,0,356976396,0],[0.75,-0.75,"0.5-1","108","-125",1,0,1318067763,1],[0.25,-0.25,"0-0.5","-179","149",1,0,1318067765,1]],[["2-2.5",2.25,"-101","-112",356976396,0]...} 

網絡頭部的屏幕截圖: Network Headers screen-shot

網絡預覽屏幕截圖: Network Preview screen-shot

通過高級REST客戶端發送相同的請求我得到正確的響應,我做錯了什麼?

感謝

+0

您是否嘗試過使用提琴手或類似工具進行調試? – Shane

+0

從來沒有聽說過小提琴手:\我現在就試試這個 –

+0

'resp = requests.get(url,params = params).json()'會發生什麼? – roganjosh

回答

0

你應該把與餅乾GET請求:

首先轉到瀏覽器鏈接和使用任何擴展,它可以讓你看到餅乾(例如餅乾怪獸)複製所有Cookie的名稱和值創造字典

cookies = { 
    'name1': 'value1', 
    'name2': 'value2' # and so on 
} 

然後只需添加餅乾,請求

resp = requests.get(url, params=params, cookies=cookies).text 

它的作品適合我。

+0

適用於我:)但餅乾不是靜態的,我是嗎?如何在每次請求時自動生成Cookie? –

+0

要自動生成Cookie,您需要執行JavaScript功能,這是您在沒有Cookie的情況下發送請求時所得到的。這應該是可能的,但我不知道如何去做 – Poloq

+0

你也可以使用硒與phantomjs或類似的東西打開瀏覽器通過python。但它會比使用請求慢。 – Poloq