Re: [問題] 該怎麼用python登入自己寫的表單?已刪文

看板Python作者 (Neisseria)時間9年前 (2016/04/13 12:27), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《phpjson (php er)》之銘言: : 大家好 : 新手在下 寫了一個測試網頁 位在 http://pure.comxa.com/index.html : 帳號是 test@test.com 密碼是 test : 登入成功 會顯示 you did it 登入失敗 會跳js窗 說error : 現在 想用python 3 練習登入 : 然而 每次都會傳回登入錯誤的頁面 不知道錯在哪 : 實在是很困惑 : 不知是否有高手能救救我呢???? >< 謝謝各位 因為程式碼本身就有錯誤了 重點在於 'action' 不是 web agent 能決定的參數 以本例來說,真正要填入的只有 'userName' 和 'pswd' 兩個 input 欄位 另外,也不需要再另外寫一個 c.get 送出 request 後,得到的就已經是 response 了 這一部分,可以看一些 HTTP 協定的書或網站,會比較容易了解 例如:http://www.tutorialspoint.com/http/ 附上範例,我自己試可成功,請自行參考 import requests res = requests.post('http://pure.comxa.com/loginSuccessDisplay.php', data={'userName': 'test@test.com', 'pswd': 'test'}) print res.text.encode('ascii', 'ignore') : 程式碼如下 : import requests : import urllib : from requests import session : headers = {'User-Agent': 'Mozilla/5.0'} : payload = { : 'action': 'loginSuccessDisplay.php', : 'userName':'test@test.com', : 'pswd':'test' : } : with session() as c: : c.post('http://pure.comxa.com/index.html',headers=headers,data=payload) : response = c.get('http://pure.comxa.com/loginSuccessDisplay.php') : # 改成index.html還是不行 會返回登入頁面@@ : print(response.headers) : print(response.text) : 話說我的網頁後台並不會產生session : 而是只是單純的驗證username pswd有沒有match : 請問這是問題癥結點嗎? : 還是我的 request 寫錯了呢? >< 懇請大大賜教謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.71.94.1 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1460521642.A.F96.html

04/14 00:59, , 1F
非常謝謝您
04/14 00:59, 1F

04/14 01:04, , 2F
看到有些人會加action: ~ 或token: ~ 還以為要加XDrz
04/14 01:04, 2F
文章代碼(AID): #1N3Sgg-M (Python)
文章代碼(AID): #1N3Sgg-M (Python)