Turn any webpage into structured data using LLMs
Turn any webpage into structured data using LLMs
LLM Scraper is a TypeScript library that allows you to extract structured data from any webpage using LLMs.
[!IMPORTANT] LLM Scraper was updated to version 2.0.
The new version comes with Vercel AI SDK 6 support and updated examples.
html for loading pre-processed HTMLraw_html for loading raw HTML (no processing)markdown for loading markdowntext for loading extracted text (using Readability.js)image for loading a screenshot (multi-modal only)custom for loading custom content (using a custom function)Make sure to give it a star!
Install the required dependencies from npm:
npm i zod playwright llm-scraperInitialize your LLM:
OpenAI
npm i @ai-sdk/openaiimport { openai } from '@ai-sdk/openai'
const llm = openai('gpt-4o')Anthropic
npm i @ai-sdk/anthropicimport { anthropic } from '@ai-sdk/anthropic'
const llm = anthropic('claude-3-5-sonnet-20240620')npm i @ai-sdk/googleimport { google } from '@ai-sdk/google'
const llm = google('gemini-1.5-flash')Groq
npm i @ai-sdk/openaiimport { createOpenAI } from '@ai-sdk/openai'
const groq = createOpenAI({
baseURL: 'https://api.groq.com/openai/v1',
apiKey: process.env.GROQ_API_KEY,
})
const llm = groq('llama3-8b-8192')Ollama
npm i ollama-ai-provider-v2import { ollama } from 'ollama-ai-provider-v2'
const llm = ollama('llama3')Create a new scraper instance provided with the llm:
import LLMScraper from 'llm-scraper'
const scraper = new LLMScraper(llm)In this example, we're extracting top stories from HackerNews:
…Output
…More examples can be found in the examples folder.
Replace your run function with stream to get a partial object stream.
// Run the scraper in streaming mode
const { stream } = await scraper.stream(page, Output.object({ schema }))
// Stream the result from LLM
for await (const data of stream) {
console.log(data.top)
}Using the generate function you can generate re-usable playwright script that scrapes the contents according to a schema.
// Generate code and run it on the page
const { code } = await scraper.generate(page, Output.object({ schema }))
const result = await page.evaluate(code)
const data = schema.parse(result)
// Show the parsed result
console.log(data.top)As an open-source project, we welcome contributions from the community. If you are experiencing any bugs or want to add some improvements, please feel free to open an issue or pull request.