我試圖使用Python requests庫登錄到名爲surfline.com的網站,然後登錄get
網頁(可能在持續session內)。如何使用Python腳本(請求庫)保持登錄到網站?
是surfline.com整個頁面使用的登錄表單使用onclick()
映射到名爲verifyLogin()
一個JS的功能,這反過來會發布到.cfm
文件。在success
上,它刷新頁面,並且只要cookie持續存在,用戶現在就保持登錄狀態。
我現在用的是requests庫的第一次,而我不能確定如何:
- 登錄成功,
- 逗留登錄在整個會議期間,
- 然後打印出網頁作爲登錄的用戶到終端。
這裏是登錄表單(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&slintcid=LOGIN&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 =假)]>
好了,餅乾似乎是當我在s.post()'調用上發佈正確的數據後,它會呈現,因此它已成功登錄。然而,第二次調用'http://www.surfline.com/home/index.cfm'仍然返回頁面,就好像用戶沒有登錄一樣。我是否必須將cookie作爲參數傳遞給第二個電話,或類似的東西? (我會在下一條評論中發佈'RequestsCookieJar',這樣你就可以看到) – sgarza62
實際上,它太長了,我將在問題 – sgarza62
中作爲編輯發佈。檢查Chrome設置,我看到Cookie是Surfline在成功登錄時放置的cookie與Session對象中的Cookie不同(例如,有一些名爲USER_ID和SETTINGSID),並且沒有LOGGED_OUT cookie。 '會話'對象。所以,我不相信登錄成功發生。是否有任何方法可以在我的機器上逐字運行JavaScript函數,並將用戶名和密碼字段寫入文件中? – sgarza62