Scraper starter
Fetch a web page and return its headlines as JSON with cheerio.
Open in editor Free. Runs in your browser, no sign-up.
- cheerio
Scraper
The code
Two files, exactly what you get in the editor.
import { createServer } from 'node:http'
import * as cheerio from 'cheerio'
// GET /?url=https://example.com returns the page title and its headings.
const server = createServer(async (req, res) => {
const target = new URL(req.url ?? '/', 'http://localhost').searchParams.get('url')
?? 'https://example.com'
try {
const html = await (await fetch(target)).text()
const $ = cheerio.load(html)
const headings = $('h1, h2, h3')
.map((_i, el) => $(el).text().trim())
.get()
.filter(Boolean)
.slice(0, 50)
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ url: target, title: $('title').text().trim(), headings }, null, 2))
} catch (error) {
res.writeHead(502, { 'content-type': 'application/json' })
res.end(JSON.stringify({ error: String(error) }))
}
})
const port = Number(process.env.PORT) || 3000
server.listen(port, () => console.log(`Scraper on port ${port}`))
Questions
Can I run Scraper in the browser?
Yes. Press Run and the program runs in a Node.js-compatible runtime, compiled to WebAssembly, right in your browser tab. npm packages install in the browser too.
How do I start a Scraper project online?
Click Open in editor. jsdrop copies this starter into a new drop with index.ts and package.json. Press Run to start it, and edit as you go.
Can other people reach my server?
Yes. When the program calls listen(), the preview shows the server and a public tunnel URL that works while your tab runs it. With Pro, deploy the drop to run around the clock at its own address.
Is it free?
Yes. Public drops, running in the browser, live collaboration, versions, and embeds are free, and you do not need an account to start.