我想通過遵循書籍「Web Scraping with Python」一書中的一個實踐示例,使用請求庫中的會話對象使用登錄憑證發出POST請求。但是,我遇到了一個奇怪的問題(可能是一個bug)。這本書的作者已經建立了一個簡單的登錄網頁進行測試: http://pythonscraping.com/pages/cookies/login.html 的用戶名可以與密碼爲「密碼」,然後在登錄過程中會被重定向到另一個頁面什麼: http://pythonscraping.com/pages/cookies/welcome.php奇怪的結果與在Python中的請求庫做網頁登錄
我用它來反對它運行Python代碼是如下:
import requests
ssn = requests.Session()
param = {'password':'password','username':'letmein'}
r = ssn.post('http://pythonscraping.com/pages/cookies/welcome.php',data=param)
print('Cookies set to :')
print(r.cookies.get_dict())
print('Server response :')
print(r.text)
和上面的代碼的輸出是:
Cookies set to :
{'loggedin': '1', 'username': 'letmein'}
Server response :
<h2>Welcome to the Website!</h2>
Whoops! You logged in wrong. Try again with any username, and the password "password" <br><a href="login.html">Log in here</a>
APPA很明顯登錄失敗了。我不明白的是爲什麼?並在程序輸出中顯示由請求設置的POST變量,密碼變量缺失。我在這裏錯過了什麼,或者它是否與請求庫有關?
P.S.我正在使用Python 3.6.2和Requests 2.18.4運行程序
試圖通過瀏覽器登錄會導致相同的消息。請求庫似乎沒有錯誤。 –
@ LokeshA.R。嗯....這很奇怪。我也嘗試過瀏覽器,它的工作原理沒有問題。 – Hapsburg