Prompt
How do I scrape a website that requires login credentials?
Latest observation
If you need to scrape a site that requires login, the safest and most reliable approach is usually to use the site’s official API or export features if they exist. If you do scrape, make sure you’re allowed to by the site’s terms and robots rules, and never try to bypass access controls.
A typical legitimate workflow looks like this:
1) Check for an API or data export
- Look for an official API, RSS feed, CSV export, or bulk download.
- These are more stable than scraping pages and less likely to break.
2) Understand the login flow
Common login types:
- Form login with cookies/session
- Single sign-on (OAuth/SAML)
- Two-factor authentication
- CSRF-protected forms
You’ll need to authenticate in a way that preserves the session.
3) Use an HTTP session and log in once
For simple form-based logins, you can usually:
- Start a persistent session
- Fetch the login page
- Extract any CSRF token
- Submit username/password
- Keep the session cookies for subsequent requests
Example pattern in Python:
import requests
from bs4 import BeautifulSoup
session = requests.Session()
# 1. Load login page
login_page = session.get("https://example.com/login")
soup = BeautifulSoup(login_page.text, "html.parser")
# 2. Extract CSRF token if present
csrf = soup.find("input", {"name": "csrf_token"})
csrf_token = csrf["value"] if csrf else None
# 3. Submit login form
payload = {
"username": "your_username",
"password": "your_password",
}
if csrf_token:
payload["csrf_token"] = csrf_token
resp = session.post("https://example.com/login", data=payload)
# 4. Verify login succeeded
print(resp.status_code, resp.url)
# 5. Use authenticated session for scraping
data = session.get("https://example.com/protected/page")
print(data.text)
4) If the site uses JavaScript-heavy login
If the login is handled by frontend JavaScript, you may need a browser automation tool like:
- Playwright
- Selenium
That lets you log in like a normal user, then read the resulting page or cookies.
5) Respect rate limits and sessions
- Don’t hammer the site.
- Add delays between requests.
- Handle expired sessions by re-authenticating.
- Store credentials securely (environment variables or a secrets manager).
6) Be careful with MFA and anti-bot systems
If the site uses MFA, CAPTCHA, or bot detection, don’t attempt to bypass those protections. Use the site’s supported access methods or ask for authorized API access.
If you want, I can show you:
- a requests-based login scraper example,
- a Playwright example for a JS login page,
- or help you adapt it to a specific site’s login form.
Observation history
Unlock observation history
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 (1 observation 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.