如何在手动访问网站时抓取Cookie并存储至Cookie Jar复用?
Hey there! I get it—transferring cookies from your manual browser sessions into a cookie jar for tools like Requests or Mechanize can feel tricky when your first attempts don’t pan out. Let’s walk through how to do this properly for both tools, plus cover common pitfalls that might be causing your issues.
For Requests
Requests uses RequestsCookieJar, which is straightforward to populate with your manual cookies. You’ve got two solid approaches:
1. Add Cookies Individually (Best for Precision)
If you copied specific cookie details (name, value, domain, path) from your browser’s dev tools (under Application > Cookies), add them one by one to ensure they match the site’s requirements:
import requests # Initialize an empty cookie jar cookie_jar = requests.cookies.RequestsCookieJar() # Add each cookie with exact domain/path from your browser cookie_jar.set('session_id', 'abc123xyz', domain='your-target-site.com', path='/') cookie_jar.set('user_theme', 'dark', domain='your-target-site.com', path='/settings') # Use the jar in your request response = requests.get('https://your-target-site.com/protected-content', cookies=cookie_jar)
2. Parse a Copied Cookie String
If you copied the raw cookie string from your browser (looks like name1=val1; name2=val2; ...), split and add them quickly:
import requests cookie_jar = requests.cookies.RequestsCookieJar() raw_cookie_str = "session_id=abc123xyz; user_theme=dark; csrf_token=def456" # Split and add each cookie to the jar (handles values with '=' by splitting only once) for cookie_pair in raw_cookie_str.split('; '): name, value = cookie_pair.split('=', 1) cookie_jar.set(name, value) # Send your request with the populated jar response = requests.get('https://your-target-site.com/protected-content', cookies=cookie_jar)
For Mechanize
Mechanize’s cookie jar requires proper Cookie objects with specific attributes to ensure they’re sent correctly. Here’s how to set it up:
import mechanize # Initialize the cookie jar and browser instance cookie_jar = mechanize.CookieJar() browser = mechanize.Browser() browser.set_cookiejar(cookie_jar) # Add a manual cookie (fill in details from your browser's dev tools) new_cookie = mechanize.Cookie( version=0, name='session_id', value='abc123xyz', domain='your-target-site.com', domain_specified=True, path='/', path_specified=True, secure=False, # Set to True if the cookie is marked as Secure in your browser expires=None, # Leave as None for session-only cookies discard=True ) cookie_jar.set_cookie(new_cookie) # Or parse a raw cookie string (wrap each in a Mechanize Cookie object) raw_cookie_str = "session_id=abc123xyz; csrf_token=def456" for cookie_pair in raw_cookie_str.split('; '): name, value = cookie_pair.split('=', 1) cookie = mechanize.Cookie( version=0, name=name, value=value, domain='your-target-site.com', path='/', secure=False, expires=None ) cookie_jar.set_cookie(cookie) # Use the browser to access protected content—cookies will be sent automatically response = browser.open('https://your-target-site.com/protected-content')
Common Reasons Your Attempts Might Have Failed
- Mismatched Domain/Path: Cookies are tied to specific domains and paths. If you add a cookie for
your-target-site.combut requestsub.your-target-site.com, or the cookie’s path is/adminbut you hit/, the cookie won’t be sent. Always cross-check these values with your browser’s dev tools. - Secure/HttpOnly Flags: If the cookie is marked as
Secure, you must use HTTPS in your request.HttpOnlyblocks JS access but still requires you to include the flag in the cookie object if your browser shows it. - Expired Cookies: Make sure the cookies you’re copying are still valid—check the
ExpiresorMax-Agefield in your browser’s cookie list.
内容的提问来源于stack exchange,提问作者PygmyPony

