如何从响应头、认证信息及指定站点获取全部Cookie?
Alright, let's walk through exactly how to pull all cookies from response headers, authentication flows, and those specific Brobizz self-service sites using your defined constants. Here's a practical, step-by-step breakdown:
Core Concept
To grab all relevant cookies, you'll need to:
- First hit the login page to collect initial session cookies (sent via
Set-Cookieresponse headers). - If authentication is required, send your credentials to the API endpoint to get authenticated session cookies.
- Either let a HTTP client handle cookie persistence automatically, or manually parse the
Set-Cookieheaders to extract every cookie.
Implementation with Your Constants
First, let's lay out your defined constants (I filled in a common User-Agent for completeness):
# Your defined constants LoginPage = "https://my.brobizz.com/selfservice/login/?language=en" PostPage = "https://my.brobizz.com/selfservice/api" Accept = "application/json, text/javascript, */*; q=0.01" Accept_Language = "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3" Accept_Encoding = "gzip, deflate, br" X_Requested_With = "XMLHttpRequest" Referer = "https://my.brobizz.com/selfservice/login/?language=en" User_Agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0"
Step 1: Set Up Request Headers
Build the headers object using your constants to mimic a real browser request:
headers = { "Accept": Accept, "Accept-Language": Accept_Language, "Accept-Encoding": Accept_Encoding, "X-Requested-With": X_Requested_With, "Referer": Referer, "User-Agent": User_Agent }
Step 2: Fetch Cookies with a Persistent Session
Using Python's requests library (a go-to for HTTP tasks), we'll use a Session to automatically persist cookies across requests—this saves you from manually tracking every cookie:
import requests # Initialize a session to keep cookies between requests session = requests.Session() # 1. Get initial cookies from the login page login_response = session.get(LoginPage, headers=headers) # Extract all cookies stored in the session (includes all from response headers) initial_cookies = session.cookies.get_dict() print("Initial cookies from login page:", initial_cookies) # Optional: View raw Set-Cookie headers if you need to parse manually raw_set_cookies = login_response.headers.getlist("Set-Cookie") print("\nRaw Set-Cookie headers from login page:") for cookie in raw_set_cookies: print(cookie) # 2. If you need to authenticate, send your credentials to the API endpoint # Replace the payload with your actual login data auth_payload = { "username": "your_brobizz_username", "password": "your_brobizz_password" } auth_response = session.post(PostPage, headers=headers, json=auth_payload) # Get all cookies after authentication (includes new session cookies) authenticated_cookies = session.cookies.get_dict() print("\nAll cookies after authentication:", authenticated_cookies) # Optional: View raw Set-Cookie headers from the auth response auth_raw_cookies = auth_response.headers.getlist("Set-Cookie") print("\nRaw Set-Cookie headers from auth response:") for cookie in auth_raw_cookies: print(cookie)
Key Tips
- Session Persistence: Using
requests.Session()is the easiest way to manage cookies—every subsequent request will automatically send the cookies you've collected. - Manual Parsing: If you need to extract specific cookie attributes (like expiration date or domain), split each
Set-Cookiestring by semicolons to break down the key-value pairs. - User-Agent: Make sure your
User-Agentmatches the one your actual browser uses—some sites block requests with incomplete or generic UA strings.
内容的提问来源于stack exchange,提问作者Dmitrij Holkin




