如何使用Python的請求模塊“登錄”到網站?我試圖發(fā)布一個使用Python中的請求模塊登錄到網站的請求,但它并不真正有效。我對此并不熟悉.所以我不知道是否應該制作用戶名和密碼cookie,或者我找到的某種類型的HTTP授權(?)。from pyquery import PyQueryimport requests
url = 'http://www.locationary.com/home/index2.jsp'所以現(xiàn)在,我想我應該用“帖子”和“餅干”.ck = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}r = requests.post(url, cookies=ck)content = r.text
q = PyQuery(content)title = q("title").text()print title我有種感覺我把餅干做錯了.我不知道。如果沒有正確登錄,主頁的標題應該是“Locationary.com”,如果是的話,應該是“Home Page”。如果您能向我解釋一些關于請求和cookie的事情,并幫助我解決這個問題,我將非常感激。d:謝謝。.還沒成功好的.這就是主頁HTML在登錄之前說的:</td><td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_email.gif"> </td><td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName" size="25"></td><td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_password.gif"> </td><td><input class="Data_Entry_Field_Login" type="password" name="inUserPass" id="inUserPass"></td>所以我認為我做得對,但輸出仍然是“Locationary.com”。第二版編輯:我希望能夠在很長一段時間內保持登錄,每當我請求在該域下的一個頁面,我希望內容顯示,就像我已登錄。
3 回答

慕尼黑5688855
TA貢獻1848條經驗 獲得超2個贊
requests.Session()
例
import requests# Fill in your details here to be posted to the login form.payload = { 'inUserName': 'username', 'inUserPass': 'password'}# Use 'with' to ensure the session context is closed after use.with requests.Session() as s: p = s.post('LOGIN_URL', data=payload) # print the html returned or something more intelligent to see if it's a successful login page. print p.text # An authorised request. r = s.get('A protected web page url') print r.text # etc...
添加回答
舉報
0/150
提交
取消