Prompt
How do I scrape JavaScript-heavy websites?
Latest observation
Scraping JavaScript-heavy websites usually means you need to render the page like a real browser, not just fetch the raw HTML. Here are the common approaches, from simplest to most robust:
1) First check if there’s an API behind the site
Many “dynamic” sites still load data from JSON endpoints.
How to look:
- Open DevTools → Network
- Reload the page
- Filter by Fetch/XHR
- Look for requests returning JSON
If you find an endpoint, it’s often much easier to scrape that directly with requests/httpx than to automate a browser.
2) Use a headless browser for rendering
If content is created by JavaScript in the browser, use:
- Playwright (recommended)
- Selenium
- Puppeteer (Node.js)
These tools load the page, execute JavaScript, and let you extract the rendered DOM.
Example with Playwright (Python)
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://example.com", wait_until="networkidle")
html = page.content()
print(html)
browser.close()
Extract text or elements
title = page.locator("h1").inner_text()
links = page.locator("a").all_inner_texts()
3) Wait for the right thing
JavaScript apps often render content after a delay. Instead of sleeping arbitrarily, wait for:
- a selector to appear
- network to go idle
- a specific text value
Examples:
page.wait_for_selector(".product-card")
page.wait_for_load_state("networkidle")
4) Handle scrolling, pagination, and lazy loading
Some sites only load more content when you scroll.
You may need to:
- scroll to the bottom
- click “Load more”
- paginate through routes or query params
Example:
page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
5) If the site uses anti-bot protections
Some JS-heavy sites also use:
- rate limiting
- CAPTCHAs
- fingerprinting
- auth tokens / session cookies
Be careful to:
- respect the site’s ToS and robots.txt where applicable
- throttle requests
- reuse cookies/sessions
- avoid aggressive scraping
If you’re blocked, you may need:
- a logged-in session
- browser context persistence
- stealth/fingerprint mitigation
- official APIs or data exports
6) For structured scraping, use browser automation + parsing
Typical pipeline:
- Load page in Playwright/Selenium
- Wait for data to render
- Extract HTML or element text
- Parse with BeautifulSoup/lxml if needed
- Save results to CSV/JSON
7) Consider alternatives
If you need to scrape at scale, browser automation can be slow and expensive. Alternatives:
- direct API calls
- server-side rendering endpoints
- browserless services
- data providers / feeds
Recommended stack
- Playwright for dynamic pages
- BeautifulSoup or lxml for parsing extracted HTML
- httpx/requests for direct API calls
- Redis / queues for large-scale jobs
If you want, I can give you:
- a Playwright scraper template in Python,
- a Selenium version, or
- help you inspect a specific site’s network requests.
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.