Skip to content

Turn any webpage into structured data using LLMs

License

Notifications You must be signed in to change notification settings

ForkKvisaz/llm-scraper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LLM Scraper

Screenshot 2024-04-20 at 22 08 44

LLM Scraper is a TypeScript library that allows you to convert any webpages into structured data using LLMs.

Tip

Under the hood, it uses function calling to convert pages to structured data. You can find more about this approach here

Features

  • Uses OpenAI chat models
  • Schemas defined with Zod
  • Full type-safety with TypeScript
  • Based on Playwright framework
  • Supports 3 operating modes: html, text, image
  • Streaming when crawling multiple pages

Make sure to give it a star!

Screenshot 2024-04-20 at 22 13 32

Getting started

  1. Install the required dependencies from npm:

    npm i zod playwright llm-scraper
    
  2. Get an OpenAI API key and set it in your environment variables:

    export OPENAI_API_KEY=***
    
  3. Optional. Create a new browser instance and attach LLMScraper to it:

    import { chromium } from 'playwright'
    import LLMScraper from 'llm-scraper'
    
    const browser = await chromium.launch()
    const scraper = new LLMScraper(browser)

Example

In this example, we're extracting top stories from HackerNews:

import z from 'zod'
import { chromium } from 'playwright'
import LLMScraper from 'llm-scraper'

// Create a new browser instance
const browser = await chromium.launch()

// Initialize the LLMScraper instance
const scraper = new LLMScraper(browser)

// Define schema to extract contents into
const schema = z.object({
  top: z
    .array(
      z.object({
        title: z.string(),
        points: z.number(),
        by: z.string(),
        commentsURL: z.string(),
      })
    )
    .describe('Top stories on Hacker News'),
})

// URLs to scrape
const urls = ['https://news.ycombinator.com']

// Run the scraper
const pages = await scraper.run(urls, {
  model: 'gpt-4-turbo',
  schema,
  mode: 'html',
  closeOnFinish: true,
})

// Stream the result from LLM
for await (const page of pages) {
  console.log(page.data)
}

Contributing

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.

About

Turn any webpage into structured data using LLMs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%