Prompt

How do you scrape data from pages that require login?

Latest observation

Aug 27, 2026 · Gemini APIWeb search: off

Scraping data from behind a login wall requires your scraper to authenticate and maintain a session (via cookies, tokens, or headers) just like a real user browser does.

Here is a comprehensive guide to the four main methods used to scrape authenticated pages, ranging from the easiest to the most advanced.


Key Concept: Sessions and Cookies

When you log into a website, the server sends back a Session ID or an Authentication Token (usually stored as a Cookie or Local Storage). For every subsequent request, your browser automatically sends this cookie back to prove you are logged in. Scraping logged-in pages relies on simulating this process.


Method 1: Exporting Cookies from Browser (Easiest)

If you just need to run a quick script, you don't need to write code to log in. You can log in manually using your normal browser, export your session cookies, and load them into your script.

Steps:

  1. Log into the target site in Chrome/Firefox.
  2. Use an extension like EditThisCookie or open Browser Developer Tools -> Application -> Cookies.
  3. Copy the session cookie values (e.g., sessionid, remember_token).
  4. Pass those cookies into your scraping request.

Python Example (requests):

import requests
from bs4 import BeautifulSoup

# Define cookies exported from your browser
cookies = {
    "session_id": "xyz123abc456...",  # Replace with real cookie name and value
}

# Add headers to look like a real browser
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}

# Make request using cookies
url = "https://example.com/protected-dashboard"
response = requests.get(url, cookies=cookies, headers=headers)

soup = BeautifulSoup(response.text, "html.parser")
print(soup.title.text)

Method 2: Programmatic Login using HTTP Sessions (Fastest)

If you need an automated process that logs in automatically without manual intervention, use a Session object. This maintains cookies across multiple requests automatically.

Steps:

  1. Open Chrome DevTools (F12), go to the Network tab, and check "Preserve log".
  2. Log into the website manually.
  3. Look for the POST request sent to the login URL. Inspect the Payload / Form Data to see what data was sent (e.g., username, password, _csrf_token).
  4. Replicate that POST request in your code.

Python Example (requests.Session):

import requests
from bs4 import BeautifulSoup

session = requests.Session()

# Optional: Get login page first if there is a hidden CSRF token required
login_page_url = "https://example.com/login"
res = session.get(login_page_url)
soup = BeautifulSoup(res.text, "html.parser")

# Extract CSRF token if the site uses one
csrf_token = soup.find("input", {"name": "csrf_token"})["value"]

# Prepare login payload
payload = {
    "username": "your_username",
    "password": "your_password",
    "csrf_token": csrf_token,  # Include if applicable
}

# Perform Login (POST request)
post_url = "https://example.com/api/login"
session.post(post_url, data=payload)

# Now access the protected page (Session automatically sends authentication cookies)
dashboard_res = session.get("https://example.com/dashboard")
print(dashboard_res.text)

Method 3: Browser Automation (Best for JavaScript Heavy Sites & CAPTCHAs)

Modern websites (built with React, Vue, Angular) often use complex login flows, single-page application architecture, or anti-bot protections that break simple HTTP requests. Using tools like Playwright, Selenium, or Puppeteer controls a real browser.

Python Example (Playwright):

from playwright.sync_api import sync_playwright


def scrape_with_login():
    with sync_playwright() as p:
        # Launch browser (headless=False lets you see what happens)
        browser = p.chromium.launch(headless=False)
        context = browser.new_context()
        page = context.new_page()

        # 1. Navigate to login page
        page.goto("https://example.com/login")

        # 2. Fill credentials
        page.fill('input[name="username"]', "your_username")
        page.fill('input[name="password"]', "your_password")

        # 3. Click Login
        page.click('button[type="submit"]')

        # 4. Wait for navigation/dashboard to load
        page.wait_for_url("**/dashboard")

        # 5. Extract data or save browser session state
        content = page.content()
        print("Page loaded successfully!")

        # Save session so you don't have to log in next time
        context.storage_state(path="state.json")

        browser.close()


scrape_with_login()

Pro Tip: Playwright allows you to save your authenticated state using context.storage_state(path="state.json"). In future runs, you can load state.json to bypass the login step entirely until the session expires.


Method 4: Reverse-Engineering Hidden APIs (The Professional Way)

Most modern applications log in using JavaScript and fetch data asynchronously via JSON APIs (REST or GraphQL).

  1. Open DevTools -> Network tab -> Filter by Fetch/XHR.
  2. Log in and navigate around the page.
  3. Look for API endpoints returning JSON data (e.g., https://example.com/api/v1/user/data).
  4. Inspect the request headers for an Authorization: Bearer <TOKEN> header.
  5. Make your requests directly to these internal API endpoints using requests or httpx instead of scraping HTML.

Common Obstacles & How to Handle Them

  1. Two-Factor Authentication (2FA/MFA):

    • Solution: Automating 2FA is difficult. The best practice is to log in manually using Browser Automation (Method 3), solve the 2FA once, and save the session state/cookies locally. Reuse that saved state for future scrapers.
  2. CAPTCHAs on Login Pages:

    • Solution: Similar to 2FA, solve it manually once in a non-headless browser, save the cookies/session, and reuse them. Alternatively, use third-party CAPTCHA solving services like 2Captcha or Anti-Captcha via API.
  3. Rate Limiting & IP Blocks:

    • Solution: Use residential proxy networks and add random delays (time.sleep()) between requests to avoid triggering security filters.

Legal and Ethical Considerations

  • Terms of Service (ToS): Scraping behind a login wall often violates a site's ToS because you explicitly agreed to their rules when creating an account. Your account could be banned.
  • Rate Limits: Keep request frequency low so you don't cause a Denial of Service (DoS) for real users.
  • Data Privacy: Never scrape, store, or distribute personally identifiable information (PII) belonging to other users.

Observation history

See 2 earlier observations

Create a free Obsurfable account to unlock historical responses, model comparisons, and deeper brand intelligence.

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (3 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, and Meta AI.