原文地址:https://blog.csdn.net/jyh_jack/article/details/82457953
如何使用requests后登錄網站后,保持Session繼續實現瀏覽、下載等效果?
兩個request之前都是單獨的請求,需要保持Session,重點看下面第二段代碼。
下面的代碼,實現的是一個網站通過request的post方式,用戶名+密碼登錄。
1 import requests 2
3 url = "http://?????.com/SvltLogin"
4 response = requests.post(url, data={"txtUsr_id": "00000001", "txtPassword": "mima"}) 5 response.encoding = "GBK"
6 print(response.text)
下面的代碼,通過Session登錄,瀏覽需要登錄后的頁面
1 import requests 2
3 url = "http://?????.com/SvltLogin"
4 s = requests.session() #建立一個Session
5 response = s.post(url, data={"txtUsr_id": "00000001", "txtPassword": "mima"}) #session登錄網站
6 response = s.get("http://?????.com/SvltLogout") #session瀏覽頁面
7 response.encoding = "GBK"
8 print(response.text)