我使用要求(2.2.1)登錄網址http://tx3.netease.com/logging.php?action=login
,但這個網址的登錄邏輯不同於Django的CSRF令牌機制,即:Python請求如何調用js函數來計算post之前的某個值?
- 當你得到這個URL,有兩個導入值爲
formhash
和sts
,它們都將用於js函數do_encrypt
(文件http://tx3.netease.com/forumdata/cache/rsa/rsa_min.js
)中。這很好,我可以很容易地抓住他們。
HTML文本的關鍵部分是:
<form method="post" name="login" id="loginform" class="s_clear" onsubmit="do_encrypt('ori_password','password');pwdclear = 1;" action="logging.php?action=login&loginsubmit=yes">
<input type="hidden" name="formhash" value="91e54489" />
<input type="hidden" name="referer" value="http://tx3.netease.com/" />
<input type="hidden" name="sts" id="sts" value="1409414053" />
<input type="hidden" name="password" id="password" />
...
<input type="password" id="ori_password" name="ori_password" onfocus="clearpwd()" onkeypress="detectCapsLock(event, this)" size="36" class="txt" tabindex="1" autocomplete="off" />
...
</form>
2.輸入電子郵件和原始密碼ori_password
,點擊提交按鈕將調用do_encrypt
,將使用formhash
,sts
和ori_password
設置實時後郵政字典的密碼password
。問題出來 - 似乎沒有辦法直接得到password
字符串。 (爲了便於比較,你可以直接在Django的情況下獲得csrfmiddlewaretoken
從session_client.cookies['csrftoken']
)
這是代碼:
import requests
import json
import re
loginUrl = "http://tx3.netease.com/logging.php?action=login"
client = requests.session()
r = client.get(loginUrl)
r.encoding='gb18030'
stsPat = re.compile('<input type="hidden" name="sts" id="sts" value="(\d+?)" />')
formhashPat = re.compile('<input type="hidden" name="formhash" value="([\d\w]+?)" />')
sts = stsPat.search(r.text).groups()[0]
formhash = formhashPat.search(r.text).groups()[0]
loginData={
'username' : "[email protected]",
'password' : ..., # Set by js function do_encrypt
'referer':'/',
'loginfield':'username',
'ori_password':'', # it's `111111`, but `do_encrypt` will set it to empty.
'loginsubmit':'true',
'sts':sts,
'formhash':formhash,
}
# r = client.post(url=loginUrl,data=loginData)
您將不得不使用JS引擎*或*重新實現Python中的相同邏輯來執行JS。這兩種「請求」都不能幫助你。 – 2014-08-30 17:14:21