Python无法从网站获取Cookie问题求助(附代码)
Hey Zac, let's figure out why you're not getting cookies with your code and walk through some practical fixes:
Possible Reasons & Solutions
The target site doesn't set cookies for basic requests
Example.com is a static test site that typically doesn't set any cookies for regular GET requests. Try testing with a site that actually uses cookies (like a login-enabled platform or mainstream service). Swapping the URL might already show you cookies in the session.Your request is being flagged as a bot
Defaultrequestsheaders (like theUser-Agent) give away that this is a script, not a real browser. Many sites block or restrict bot traffic, including skipping cookie setup. Add a realistic user-agent to your request:import requests session = requests.Session() # Mimic a Chrome browser on Windows session.headers.update({ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36' }) response = session.get('https://your-target-site.com') print(session.cookies.get_dict())Cookies require JavaScript rendering
Some sites generate or set cookies client-side using JavaScript. Tools likerequestscan't execute JS, so they'll miss these cookies. For cases like this, use a browser automation tool like Playwright or Selenium to simulate a real browser:# Using Playwright (install first with `pip install playwright` then run `playwright install`) from playwright.sync_api import sync_playwright with sync_playwright() as p: browser = p.chromium.launch(headless=False) # Headless=False lets you see the browser page = browser.new_page() page.goto('https://your-target-site.com') # Extract cookies as a dictionary cookie_dict = {cookie['name']: cookie['value'] for cookie in page.context.cookies()} print(cookie_dict) browser.close()Check for redirects or failed requests
Sometimes cookies are set in a redirect response. You can check if your request was redirected by printingresponse.history— this will show you all prior responses in the chain.requestsfollows redirects by default, but if the site has unusual redirect logic, you might need to adjust settings (likeallow_redirects=Falseto inspect intermediate responses).SSL certificate issues (for other sites)
If you're testing with a site that has invalid SSL certificates,requestswill fail silently (or throw an error) and won't retrieve cookies. You can temporarily disable verification withverify=False(only for testing, not production!) to rule this out:response = session.get('https://your-target-site.com', verify=False)
Hopefully one of these fixes gets you the cookies you need!
内容的提问来源于stack exchange,提问作者Zac




