Cheerio Sandbox: Basic example
Write a Cheerio extractor against any HTML and see the JSON as you type. It runs in the same sandbox as the ScrapeNinja API.
undefinedWhat is this?
A REPL for HTML extraction. Paste the HTML of a page (from the scraper playground or your browser's "View source"), write a function that returns the data you need, and iterate until the JSON is right. Then send the same function as the extractor parameter of any ScrapeNinja request.
How to write a good extractor
- Check first whether the site has a JSON API: the Network tab of DevTools often shows one. JSON is more stable than HTML.
- Prefer stable hooks:
ids,data-*attributes, text with:contains(). Generated class names like.css-1x2y3zchange with every deploy. - Return plain data (strings, numbers, arrays, objects), not Cheerio objects.
- For pages of 1 MB and more,
cheerio.load(input, { xml: { xmlMode: false } })parses 3–5x faster. See JS extractors for the trade-offs.
Why a separate sandbox?
Writing an extractor against a live website is slow: every attempt fetches the page again, and protected sites take seconds per request. Here the HTML is fixed, so every change runs in milliseconds, and you can save the HTML and the extractor and share a link. When it works, the same function runs on the live page as the extractor of a ScrapeNinja request. Want a first draft? Let AI write the extractor.
Extracting articles from many websites
For articles, blog posts and news from hundreds of different sites, a hand-written extractor per site does not scale. Use the article extractor instead: the markdown_content preset returns the main text of any page as Markdown, with title and author.
Running the extractor locally
The function works with a regular cheerio install too. With Node.js 18 or newer:
mkdir my-scraper && cd my-scraper
npm init -y && npm i cheerioSave this as scraper.mjs, paste your function in, and run node scraper.mjs:
import * as cheerio from 'cheerio';
function extract(input, cheerio) { /* your extractor */ }
const html = await (await fetch('https://example.com')).text();
console.log(extract(html, cheerio));A plain fetch from a server gets blocked on many sites. To fetch the page with a real Chrome fingerprint and rotating proxies, send the same function to the API: the Use in the API tab above has the code.