Headless Web Scraping
Extract data from pages that resist simple HTTP requests, using scrapling's three-tier fetcher architecture and CSS-based extraction.
When to Use
- Page requires JS rendering (SPA, React, Vue)
- Site has anti-bot protections (Cloudflare Turnstile, TLS fingerprinting)
- You need structured extraction of multiple elements via CSS selectors
- Simple WebFetch or requests.get() returns empty or blocked responses
Procedure
Step 1 — Select fetcher tier
Try Fetcher first and escalate on failure:
Fetcher — static HTML, no protection (fastest)
StealthyFetcher — 403/503, Cloudflare challenge, TLS fingerprint checks (best default for modern sites)
DynamicFetcher — page loads but content area empty, or needs click/scroll; use wait_selector
- altcha proof-of-work CAPTCHA → cannot be automated; document and fall back to manual.
Step 2 — Configure the fetcher
Use the configure() method (constructor kwargs are deprecated in v0.4.x). Set timeout, retries, follow_redirects for Fetcher; add headless=True, network_idle=True, and wait_selector for the browser tiers. If the browser binary is missing, run python -m playwright install chromium.
Step 3 — Fetch and extract
API: response.find("selector") (first match), response.find_all("selector") (all), element.get("attr"), element.get_all_text() (recursive text), element.html_content (raw inner HTML). Do NOT use .css_first() — it is not part of scrapling. If find() returns None, inspect response.html_content to verify the selector; if text is empty, content may be in shadow DOM/iframe (use DynamicFetcher + wait_selector).
Step 4 — Handle failures and edge cases
Implement tier fallback (Fetcher → Stealthy → Dynamic) with CAPTCHA detection (scan text for "altcha"/"proof of work" and stop, don't retry). On 403/503 escalate; on persistent 403 across all tiers the site blocks automation — document for manual access. Increase timeout to 120s for slow CDNs; add cookies/auth for session-gated sites.
Step 5 — Rate limiting and ethics
Check robots.txt (urllib.robotparser) before bulk runs and respect Disallow. Minimum 1s delay between requests; descriptive User-Agent; cache responses; do not scrape personal data without legal basis; stop immediately on 429 (raise delay to 3-5s) or IP ban.
Validation
Common Pitfalls
- Using
.css_first() (AttributeError) instead of .find().
- Starting with DynamicFetcher (10-50x slower) — escalate, don't start there.
- Constructor kwargs instead of
configure().
- Ignoring altcha CAPTCHA.
- No rate limiting (IP ban / service degradation).
- Assuming stable selectors — validate before each campaign.
Related
- use-graphql-api — prefer a GraphQL endpoint over scraping when available
- serialize-data-formats — convert extracted data to JSON/CSV
1---2name: headless-web-scraping3description: "Use when simple HTTP/WebFetch is insufficient — JS-rendered SPAs, Cloudflare/anti-bot-protected sites, or structured multi-element extraction needing DOM traversal. Picks the right scrapling fetcher tier (HTTP, stealth Chromium, or full browser), extracts with CSS selectors, and scrapes ethically. Triggers: web scraping, headless browser, scrapling, Cloudflare, JS-rendered, extract data, parse HTML, CSS selector, anti-bot."4version: 1.0.05license: MIT6tags: [web-scraping, headless, scrapling, automation, data-extraction, css-selectors, anti-bot, python]7source: https://github.com/pjt222/agent-almanac/tree/main/skills/headless-web-scraping8prerequisites: ["python", "pip install scrapling", "python -m playwright install chromium (for stealth/dynamic tiers)"]9derived_from: awesomeclaude10metadata:11 source: https://skillmd.com/skills/aashutosh396/headless-web-scraping12---1314# Headless Web Scraping1516Extract data from pages that resist simple HTTP requests, using scrapling's three-tier fetcher architecture and CSS-based extraction.1718## When to Use1920- Page requires JS rendering (SPA, React, Vue)21- Site has anti-bot protections (Cloudflare Turnstile, TLS fingerprinting)22- You need structured extraction of multiple elements via CSS selectors23- Simple WebFetch or requests.get() returns empty or blocked responses2425## Procedure2627### Step 1 — Select fetcher tier28Try `Fetcher` first and escalate on failure:29- `Fetcher` — static HTML, no protection (fastest)30- `StealthyFetcher` — 403/503, Cloudflare challenge, TLS fingerprint checks (best default for modern sites)31- `DynamicFetcher` — page loads but content area empty, or needs click/scroll; use `wait_selector`32- altcha proof-of-work CAPTCHA → cannot be automated; document and fall back to manual.3334### Step 2 — Configure the fetcher35Use the `configure()` method (constructor kwargs are deprecated in v0.4.x). Set `timeout`, `retries`, `follow_redirects` for Fetcher; add `headless=True`, `network_idle=True`, and `wait_selector` for the browser tiers. If the browser binary is missing, run `python -m playwright install chromium`.3637### Step 3 — Fetch and extract38API: `response.find("selector")` (first match), `response.find_all("selector")` (all), `element.get("attr")`, `element.get_all_text()` (recursive text), `element.html_content` (raw inner HTML). Do NOT use `.css_first()` — it is not part of scrapling. If `find()` returns None, inspect `response.html_content` to verify the selector; if text is empty, content may be in shadow DOM/iframe (use DynamicFetcher + wait_selector).3940### Step 4 — Handle failures and edge cases41Implement tier fallback (Fetcher → Stealthy → Dynamic) with CAPTCHA detection (scan text for "altcha"/"proof of work" and stop, don't retry). On 403/503 escalate; on persistent 403 across all tiers the site blocks automation — document for manual access. Increase timeout to 120s for slow CDNs; add cookies/auth for session-gated sites.4243### Step 5 — Rate limiting and ethics44Check `robots.txt` (urllib.robotparser) before bulk runs and respect Disallow. Minimum 1s delay between requests; descriptive User-Agent; cache responses; do not scrape personal data without legal basis; stop immediately on 429 (raise delay to 3-5s) or IP ban.4546## Validation4748- [ ] Correct fetcher tier (not over/under-powered)49- [ ] `configure()` used, not deprecated constructor kwargs50- [ ] Selectors verified against actual page source51- [ ] `.find()` / `.find_all()` used (not `.css_first()`)52- [ ] CAPTCHA detection in place (altcha reported, not retried)53- [ ] Rate limiting + robots.txt checked for bulk54- [ ] Extracted data non-empty and structurally correct5556## Common Pitfalls5758- Using `.css_first()` (AttributeError) instead of `.find()`.59- Starting with DynamicFetcher (10-50x slower) — escalate, don't start there.60- Constructor kwargs instead of `configure()`.61- Ignoring altcha CAPTCHA.62- No rate limiting (IP ban / service degradation).63- Assuming stable selectors — validate before each campaign.6465## Related6667- use-graphql-api — prefer a GraphQL endpoint over scraping when available68- serialize-data-formats — convert extracted data to JSON/CSV