python使用session的保存cookie狀態繼續請求接口


import requests

headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36",

}
# 創建session會話,登錄成功后會保存登錄狀態
session = requests.Session()
res = session.post(headers=headers, data={"userName": "example", "password": "123456"},
url="http://www.exapmle.com/login")
print(res.text)

headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36",
}

# 繼續訪問登陸后的接口
res = session.get(url="http://www.exapmle.com/select?type=PC&Code=123",
headers=headers)
# 此時登錄狀態了
print(res.text)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM