ScrapeGraphAI
An open-source Python library that uses LLMs and graph logic to build intelligent web scraping pipelines β scrape any website with just a natural language prompt.
π What is ScrapeGraphAI?
ScrapeGraphAI is an open-source Python library that revolutionizes web scraping by combining Large Language Models with graph-based pipelines. Instead of writing complex CSS selectors, XPath queries, or regex patterns, you simply describe what data you want in natural language, and the LLM extracts it automatically.
Natural Language
Describe what you need
Graph Pipelines
Modular node-based flows
Multi-LLM
OpenAI, Gemini, Ollama
Open Source
MIT License
π‘ Key Insight: Traditional scraping breaks when websites change layout. ScrapeGraphAI uses LLMs to understand the page semantically, making it resilient to layout changes β the LLM adapts automatically.
β‘ Traditional Scraping vs ScrapeGraphAI
β Traditional (BeautifulSoup/Scrapy)
from bs4 import BeautifulSoup
import requests
resp = requests.get(url)
soup = BeautifulSoup(resp.text, 'html.parser')
# Fragile selectors that break easily
titles = soup.select('div.product-card h2.title')
prices = soup.select('span.price-current')
ratings = soup.select('div.star-rating')
- β’ Breaks when HTML structure changes
- β’ Requires deep knowledge of CSS/XPath
- β’ Manual maintenance per site
β ScrapeGraphAI
from scrapegraphai.graphs import SmartScraperGraph
graph = SmartScraperGraph(
prompt="Extract all product names, "
"prices, and ratings",
source=url,
config={"llm": {"model": "gpt-4o-mini"}}
)
result = graph.run()
- β’ Adapts to layout changes automatically
- β’ Natural language β no selectors needed
- β’ Works across different sites
π Available Graph Pipelines
ScrapeGraphAI provides several pre-built graph pipelines for different scraping scenarios:
| Graph Type | Input Source | Description | Use Case |
|---|---|---|---|
| SmartScraperGraph | Single URL | Scrapes one page and extracts data via LLM | Product page, article |
| SearchGraph | Search query | Searches the web first, then scrapes results | Research, market analysis |
| SpeechGraph | URL β Audio | Scrapes content and generates audio summary | Podcast content, accessibility |
| ScriptCreatorGraph | URL | Generates a Python scraping script (not using LLM at runtime) | Production scripts, cost reduction |
| SmartScraperMultiGraph | Multiple URLs | Scrapes multiple pages in parallel | Bulk extraction, comparison |
| JSONScraperGraph | JSON / API | Extracts from JSON data using natural language | API responses, structured data |
| XMLScraperGraph | XML data | Parses XML documents with LLM understanding | RSS feeds, SOAP APIs, configs |
π» Code Examples
1. Basic Smart Scraper
from scrapegraphai.graphs import SmartScraperGraph
# Configuration
config = {
"llm": {
"api_key": "your-openai-key",
"model": "openai/gpt-4o-mini",
},
"verbose": True,
"headless": True, # Run browser in headless mode
}
# Create and run the scraper
smart_scraper = SmartScraperGraph(
prompt="Extract the title, author, publication date, "
"and a brief summary of the main article",
source="https://example.com/blog/article",
config=config
)
result = smart_scraper.run()
print(result)
# Output: {"title": "...", "author": "...", "date": "...", "summary": "..."}
2. Using Local LLMs (Ollama)
from scrapegraphai.graphs import SmartScraperGraph
# Use Ollama for local, private scraping (no API costs)
config = {
"llm": {
"model": "ollama/llama3",
"temperature": 0.0,
"base_url": "http://localhost:11434",
},
"embeddings": {
"model": "ollama/nomic-embed-text",
"base_url": "http://localhost:11434",
},
"verbose": True,
}
scraper = SmartScraperGraph(
prompt="List all job positions with title, location, "
"salary range, and required experience",
source="https://example.com/careers",
config=config
)
jobs = scraper.run()
for job in jobs.get("positions", []):
print(f"π {job['title']} - {job['location']} - {job['salary']}")
3. Multi-Page Scraping
from scrapegraphai.graphs import SmartScraperMultiGraph
config = {
"llm": {
"model": "openai/gpt-4o-mini",
"api_key": "your-key"
},
}
# Scrape multiple competitor product pages at once
urls = [
"https://competitor1.com/product",
"https://competitor2.com/product",
"https://competitor3.com/product",
]
multi_scraper = SmartScraperMultiGraph(
prompt="Extract product name, price, key features, "
"and customer rating",
source=urls,
config=config
)
results = multi_scraper.run()
# Compare prices and features across competitors
π€ Supported LLM Providers
βοΈ Cloud APIs
- β’ OpenAI (GPT-4o, GPT-4o-mini)
- β’ Google Gemini
- β’ Anthropic Claude
- β’ Azure OpenAI
- β’ Groq (ultra-fast)
π₯οΈ Local (Self-Hosted)
- β’ Ollama (Llama 3, Mistral, etc.)
- β’ HuggingFace Transformers
- β’ LM Studio
- β’ llama.cpp / GGUF models
π Embeddings
- β’ OpenAI Embeddings
- β’ Ollama nomic-embed-text
- β’ HuggingFace models
- β’ Google Gecko embeddings
βοΈ How It Works Internally
βββββββββββββββ ββββββββββββββββ βββββββββββββββ βββββββββββββββ
β Fetch ββββββΆβ Parse ββββββΆβ RAG / ββββββΆβ Generate β
β Node β β Node β β Chunk Node β β Node β
β β β β β β β β
β Download β β HTML β text β β Split into β β LLM extractsβ
β the page β β Clean noise β β chunks + β β structured β
β (headless) β β Extract body β β embed them β β data via β
β β β β β β β prompt β
βββββββββββββββ ββββββββββββββββ βββββββββββββββ βββββββββββββββ
Fetch Node
Downloads the page using Playwright (headless browser) or simple HTTP requests. Handles JavaScript-rendered content, cookies, and authentication.
Parse Node
Converts raw HTML to clean text. Removes navigation, ads, and boilerplate. Extracts the meaningful content from the page body.
RAG / Chunk Node
For large pages, splits content into chunks and uses embeddings to find the most relevant sections for the user's prompt. Reduces token usage.
Generate Node
Sends the relevant content + user prompt to the LLM, which extracts structured data (JSON) according to the natural language description.
π― Use Cases
Price Monitoring
Track competitor prices across e-commerce sites. Automatic adaptation to layout changes.
News Aggregation
Extract headlines, summaries, and key facts from news sites for monitoring dashboards.
Research & Analysis
Gather data from academic papers, patents, or technical docs for analysis.
Lead Generation
Extract company info, contacts, and job listings from business directories.
Review Monitoring
Collect and analyze product reviews, ratings, and customer feedback at scale.
Data Enrichment
Enrich CRM data by scraping company websites for details like tech stack, size, and funding.
β Best Practices
Do's
- Use gpt-4o-mini or local models for cost-effective bulk scraping
- Be specific in your prompt β describe the exact fields you want
- Use ScriptCreatorGraph for production to avoid per-run LLM costs
- Respect robots.txt and rate-limit your requests
- Cache results to avoid redundant LLM calls
Don'ts
- Use GPT-4 for every single page scrape (use mini models)
- Scrape pages too aggressively without delays
- Trust LLM output blindly β always validate extracted data
- Ignore legal considerations (ToS, GDPR, copyright)
- Skip error handling β sites can block, timeout, or change
π Resources
Related Topics
Test Your Knowledge
Score 8/10 or higher to pass
You need to be logged in to take this quiz.
Login to Continue