我一直在嘗試登錄到.aspx站點(https://web.iress.com.au/html/LogonForm.aspx - 用於源/初始cookie引用),它使用javascript函數__doPostBack(eventTarget, eventArgument)
來提交形式(非常有限的JavaScript知識 - 所以最好的猜測)。無法從.aspx登錄頁面使用Python和請求模塊獲得所需響應
我目前對HTTP請求的理解是,在表單的上下文中,它們主要是POST類型的請求。我用Chrome瀏覽器嗅出請求頭和形成時,我的憑據(爲了安全起見)不輸入使用的數據和有如下幾點:
Remote Address:##BLANKEDOUT##
Request URL:https://web.iress.com.au/html/logon.aspx
Request Method:POST
Status Code:302 Found
**Request Headers**
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:585
Content-Type:application/x-www-form-urlencoded
Cookie:ASP.NET_SessionId=##SESSION ID STRING##
Host:web.iress.com.au
Origin:https://web.iress.com.au
Pragma:no-cache
Referer:https://web.iress.com.au/html/LogonForm.aspx
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/##ADDRESS## Safari/537.36
**Form Data**
__EVENTTARGET:
__EVENTARGUMENT:
__VIEWSTATE: ##VIEWSTATE STRING##
__VIEWSTATEGENERATOR:##VIEWSTATEGENERATOR KEY##
__PREVIOUSPAGE: ##PREVIOSUPAGE STRING##
__EVENTVALIDATION: ##STRING##
fu:LogonForm.aspx
su:Default.aspx
un: # Would be my username if i had typed it in
pw: # Would be password
ImageButton1.x:45 # These two values change depending on where i click the submit button
ImageButton1.y:13
,這是我使用的嘗試代碼登錄:
from requests import session
payload = {
'__EVENTTARGET' : '',
'__EVENTARGUMENT' : '',
'__VIEWSTATE' : '##STRING FOUND FROM CHROME SNIFF##',
'__VIEWSTATEGENERATOR' : '##STRING FOUND FROM CHROME SNIFF##',
'__PREVIOUSPAGE' : '##STRING FOUND FROM CHROME SNIFF##',
'__EVENTVALIDATION' : '##STRING FOUND FROM CHROME SNIFF##',
'fu' : 'LogonForm.aspx',
'su' : 'Default.aspx',
'un' : '[email protected]',
'pw' : 'mypassword',
'ImageButton1.x' : '0',
'ImageButton1.y' : '0'
}
requestheaders = {
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding' : 'gzip,deflate',
'Accept-Language' : 'en-US,en;q=0.8',
'Cache-Control' : 'no-cache',
'Connection' : 'keep-alive',
'Content-Type' : 'application/x-www-form-urlencoded',
'Host' : 'web.iress.com.au',
'Origin' : 'https://web.iress.com.au',
'Cookie' : '',
'Pragma' : 'no-cache',
'Referer' : 'https://web.iress.com.au/html/LogonForm.aspx',
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/##ADRESSS AS ABOVE## Safari/537.36'
}
with session() as sesh:
LOGINURL = 'https://web.iress.com.au/html/LogonForm.aspx'
sesh.get(LOGINURL) #Get request to get the session ID cookie
sessionID = sesh.cookies['ASP.NET_SessionId'] #Grab session ID value
sessionIDname = 'ASP.NET_SessionId='
sessionIDheader = str(sessionIDname + sessionID) #Prepare session ID header
requestheaders['Cookie'] = sessionIDheader # Add session ID header to requestheaders dictionary
response = sesh.post('https://web.iress.com.au/html/LogonForm.aspx', data=payload, headers=requestheaders)
print(response.headers)
print(response.content)
我似乎得到的是頁面(https://web.iress.com.au/html/LogonForm.aspx)的內容和它的頭文件作爲響應的來源。我不確定它是否與__VARIABLES
有任何關係,但它們似乎沒有變化,previouspage
是例外。我可能需要提取這些__VARIABLES
才能在我的請求標題中使用它們嗎?
哦,我剛剛注意到 - 它可能與我的請求URL與通過Chrome瀏覽的請求URL有所不同嗎? (如果這是錯誤,拍攝我) – Sighonide 2014-09-22 13:25:23
我不會手動設置Cookie標題;爲Host,Content-Type,CacheControl,Origin,Pragma和Accept *頭文件提供saem。必要時將這些留給「請求」。 – 2014-09-22 13:25:42
是的,你需要發佈到'https:// web.iress.com.au/html/logon.aspx',而不是'LogonForm.aspx'。先生,你想用什麼槍擊? – 2014-09-22 13:26:44