2014-02-17 42 views
1

我試圖使用Python requests庫登錄到名爲surfline.com的網站,然後登錄get網頁(可能在持續session內)。如何使用Python腳本(請求庫)保持登錄到網站?

是surfline.com整個頁面使用的登錄表單使用onclick()映射到名爲verifyLogin()一個JS的功能,這反過來會發布到.cfm文件。在success上,它刷新頁面,並且只要cookie持續存在,用戶現在就保持登錄狀態。

我現在用的是requests庫的第一次,而我不能確定如何:

  1. 登錄成功,
  2. 逗留登錄在整個會議期間,
  3. 然後打印出網頁作爲登錄的用戶到終端。

這裏是登錄表單(HTML):

<form id="loginForm"> 
    <label for="name">Email Address:</label><br> 
    <input type="text" name="username" id="username"> 
    <label for="mail">Password:</label><br> 
    <input type="password" name="password" id="password"> 
    <input type="hidden" name="top_login" id="top_login" value="true"> 
    <button class="surfline-button blue1" type="button" onclick="verifyLogin();">Log In</button> 
    <button class="surfline-button grey1" type="button" onclick="jQuery('#dialog-login').dialog('close');">Cancel</button> 
    <div id="forgot">Forgot Password? <a href="/myaccount/?action=forgot_password">Click Here</a></div> 
    <div class="clear"></div> 
    <div><input style="float:left; width:18px; height:18px; margin-right:6px; border:none" type="checkbox" name="rememberMe" id="rememberMe" value="true" checked="checked"><div id="remember-me-text" style="float:left; width:200px; text-align:left; margin-top:1px;">Remember me on this computer?</div></div> 
    <div class="clear"></div> 
    <p>Not a Premium member? <a href="https://www.surfline.com/subscribe_vindicia/index.cfm?mkt=login&amp;slintcid=LOGIN&amp;slcmpname=LOGIN-MODAL">TRY PREMIUM FREE NOW</a></p> 
</form> 

這裏是verifyLogin()功能在/05222013_slmenu.js文件:

function verifyLogin(){ 
    //var username = jQuery("#username").val(); 
    //var password = jQuery("#password").val(); 
    var usernameVal = jQuery("#username").val(); 
    var passwordVal = jQuery("#password").val(); 
    var rememberMeVal = jQuery("#rememberMe").val();  
    var top_loginVal = jQuery("#top_login").val(); 

    if(usernameVal.length === 0 || passwordVal.length === 0){ 
     jQuery("#login-note").addClass("warning"); 
     if(usernameVal.length === 0){ jQuery("#username").addClass('warning'); jQuery("#login-note").html("Email Field is Blank"); }else{ jQuery("#username").removeClass('warning');} 
     if(passwordVal.length === 0){ jQuery("#password").addClass('warning'); jQuery("#login-note").html("Password Field is Blank"); }else{ jQuery("#password").removeClass('warning'); } 
     if(usernameVal.length === 0 && passwordVal.length === 0){jQuery("#login-note").html("Email and Password Fields are Blank"); } 
    }else{ 
     jQuery("#inner-dialog").fadeOut("slow",function(){ 
      var htmlData = "<center><div style='padding-top:60px;'><h1>Verifying Login</h1><img src='/global_includes/images/ajax-loader-snake-295284.gif' style='margin-top:24px;'></div></center>";             
      jQuery("#verifying").html(htmlData).fadeIn("slow", function(){ 
       //var loginData = jQuery('#loginForm').serialize(); 

       var loginData = 'username=' + escape(usernameVal) + '&password=' + escape(passwordVal) + '&rememberMe=' + rememberMeVal + '&top_login=' + top_loginVal; 

       jQuery.ajax({ 
        type:'POST', 
        url: '/myaccount/inc_login_handler.cfm', 
        data:loginData, 
        cache:false, 
        success: function(response){ 
         var responseTrimmed = response.replace(/^\s+||\s+$/g,''); 
         if(responseTrimmed != true){  
          jQuery("#verifying").fadeOut("slow",function(){ jQuery("#inner-dialog").fadeIn("slow"); jQuery("#login-note").html("Unable to find your login information. Please Try Again...").addClass("warning"); jQuery("#username").addClass('warning'); jQuery("#password").addClass('warning'); }); 
         }else{ 
          var successData = "<center><div style='padding-top:60px;'><h1>Success!</h1><img src='/global_includes/images/checkmark.png' style='margin-top:24px; margin-bottom:20px;'><br /> Please Wait, Site Reloading...</div></center>"; 
          jQuery("#verifying").fadeOut("slow",function(){ jQuery('#verifying').html(successData).fadeIn("slow"); }); 
          setTimeout(function(){ window.location.reload(); }, 1200); 
         } 
        }, 
        error:function (xhr, ajaxOptions, thrownError){ 
         jQuery("#verifying").fadeOut("slow",function(){ jQuery("#inner-dialog").fadeIn("slow"); jQuery("#login-note").html("There was an error finding your login infomation. Please Try Again...").addClass("warning"); }); 
        } 
       }); 
      }); 
     }) 
    } 
} 

我試過這個代碼,但不能似乎將主頁作爲登錄用戶;它仍然返回因爲它會的人誰沒有登錄(我的全名應該出現在標題等):

>>> import requests, json 
>>> s = requests.Session() 
>>> page_signed_out = s.get('http://www.surfline.com/home/index.cfm') 
>>> form_data = {'type':'POST', 
...    'url':'/myaccount/inc_login_handler.cfm',    
...    'data':"[email protected]&password=mypassword&rememberMe=true&top_login=true", 
...    'cache':False} 
>>> s.post(url, 
...  data=json.dumps(form_data), 
...  headers= {'content-type': 'application/json'}) 
<Response [200]> 
>>> page_signed_in = s.get('http://www.surfline.com/home/index.cfm') 

基本上,我怎麼可以登錄到surfline.com從一個Python文件,並以登錄用戶身份獲取頁面?如果requests庫不可用,我不介意使用不同的庫。謝謝。

編輯: 下面是餅乾POST已經取得後,由@安德烈

< [曲奇(版本= 0,名稱= 'CRYPTOPASS',值='%296%25所建議的%2A%2521B%3FO%2A%3C%28',port = None,port_specified = False,domain ='。surfline.com',domain_specified = True,domain_initial_dot = True,path ='/',path_specified = True,安全= False,expires = 2338694583,discard = False,comment = None,comment_url = None,rest = {},rfc2109 = False),Cookie(version = 0,name ='CRYPTOUSER',value = '23%252600%2E3Z% 5FM%5EEZV1IK%27TFIW%3E',port = None,port_specified = False,domain ='。surfline.com',domain_specified = True,domain_initial_dot = True,path ='/',path_specified = True,secure = False,過期s = 2338694583,discard = False,comment = None,comment_url = None,rest = {},rfc2109 = False),Cookie(version = 0,name ='LOGGED_OUT',value ='true',port = None,port_specified = False,domain ='。surfline.com',domain_specified = True,domain_initial_dot = True,path ='/',path_specified = True,secure = False,過期= 2338694583,discard = False,comment = None,comment_url =無,休息= {},rfc2109 = False),Cookie(version = 0,name ='USER_ID',value ='259829',port = None,port_specified = False,domain ='。surfline.com',domain_specified = True,domain_initial_dot = true,path ='/',path_specified = True,secure = False,expires = 2338694583,discard = False,comment = None,comment_url = None,rest = {},rfc2109 = False),Cookie(version = 0,name = 'CFID',value ='437349255',port = None,port_specified = False,domain ='www.surfline.com',domain_specified = False,domain_initial_dot = False,path ='/',path_specified = True,secure = False, expires = 2338694583,discard = False,comment = None,comment_url = None,rest = {},rfc2109 = False),Cookie(version = 0,name ='CFTOKEN' ,value ='1082d1233da237c-3E4C2D43-FFC6-4ECC-1E80D9B505E495CE',port = None,port_specified = False,domain ='www。surfline.com',domain_specified = False,domain_initial_dot = False,path ='/',path_specified = True,secure = False,expires = 2338694583,discard = False,comment = None,comment_url = None,rest = {},rfc2109 =假)]>

回答

1

我試圖登錄到該網站,同時監測請求使用Firebug完成的,它看起來像數據是application/x-www-form-urlencoded格式,而不是application/json,也從Javascript代碼曲解一些數值, type,url,datacache是用於AJAX調用的參數,而不是應該發送到服務器的實際數據。

這是希望工作代碼,在POST請求中替換您的電子郵件和密碼。

from requests import Session # we can just import Session, no need to import the entire requests 

s = Session() # new session 

page_signed_out = s.get('http://www.surfline.com/home/index.cfm') # gets signed out page, this will set eventual cookies that may be needed later 

s.post("http://www.surfline.com/myaccount/inc_login_handler.cfm", {"username":"[email protected]", "password":"mypassword", "rememberMe":"true", "top_login":"true"}) # does the actual login, if successful this will set a session cookie 

page_signed_in = s.get('http://www.surfline.com/home/index.cfm') # good, we're logged in 

編輯:

或者,如果出於某種原因,surfline.com並不成功登錄你,你基本上可以複製餅乾種在你的瀏覽器,當你身體登錄並將它們「粘貼」到您的程序中,然後將它們傳遞給您的請求。

from requests import Session 

s = Session() 
s.cookies["s_cc"] = "your_session_cookie" # replace this with your actual login cookie 

page_signed_in = s.get("http://www.surfline.com") # you're logged in :) 
+0

好了,餅乾似乎是當我在s.post()'調用上發佈正確的數據後,它會呈現,因此它已成功登錄。然而,第二次調用'http://www.surfline.com/home/index.cfm'仍然返回頁面,就好像用戶沒有登錄一樣。我是否必須將cookie作爲參數傳遞給第二個電話,或類似的東西? (我會在下一條評論中發佈'RequestsCookieJar',這樣你就可以看到) – sgarza62

+0

實際上,它太長了,我將在問題 – sgarza62

+0

中作爲編輯發佈。檢查Chrome設置,我看到Cookie是Surfline在成功登錄時放置的cookie與Session對象中的Cookie不同(例如,有一些名爲USER_ID和SETTINGSID),並且沒有LOGGED_OUT cookie。 '會話'對象。所以,我不相信登錄成功發生。是否有任何方法可以在我的機器上逐字運行JavaScript函數,並將用戶名和密碼字段寫入文件中? – sgarza62

1

如果你也是一個衝浪者,並且只是試圖讓流玩了一整天,我會在檢查出的RTMP連接的閃存播放器,可以提示..

甚至更​​大提示:

tcpdump -A -n -s 0 -r file.tcpdump | grep的連接

訪問一個頁面凸輪和grep的RTMP流時

..

但你可能會去別的東西,然後這將是題外話..

+0

我爲自己構建了預測數據的不同前端表示形式(不同的佈局和增加的功能),但現在您可以爲我的下一個項目提供想法。很高興遇到另一個衝浪編碼器 - 稀有面包。 – sgarza62

+0

乾杯布拉德,這完全是我在找的! – Chaz

+0

布拉德,這種方法仍然適合你嗎? rmtp流在一段時間後似乎不起作用。也許我不清楚你是如何使用它們的。 – Chaz