Crawl4AI is an open-source AI-powered web crawling and data extraction library for Python.
It uses large language models (LLMs) to intelligently extract structured data from web pages
with minimal code. Unlike traditional scraping frameworks that rely on CSS selectors or XPath,
Crawl4AI can understand page content semantically and extract data based on natural language
descriptions of what you want.
Key features include:
- LLM-based extraction
Define what data you want in plain English and Crawl4AI uses LLMs to find and extract it
from the page content. Supports multiple LLM providers including OpenAI, Anthropic, and local models.
- Automatic crawling
Built-in crawler with support for JavaScript rendering, parallel crawling, and session management.
- Structured output
Returns data in structured formats (JSON, Pydantic models) making it easy to integrate
into data pipelines.
- Markdown conversion
Can convert web pages to clean markdown format, useful for feeding content to LLMs.
- Chunking strategies
Multiple strategies for breaking down large pages into processable chunks for LLM extraction.
- Async support
Built on async Python for efficient concurrent crawling and extraction.
Crawl4AI is particularly useful for scraping unstructured content where writing traditional
CSS/XPath selectors would be tedious or fragile. It excels at content extraction, article
parsing, and data mining from diverse page layouts.
Gracy is an API client library based on httpx that provides an extra stability layer with:
- Retry logic
- Logging
- Connection throttling
- Tracking/Middleware
In web scraping, Gracy can be a convenient tool for creating scraper based API clients.
```python
from crawl4ai import AsyncWebCrawler, CrawlerRunConfig
from crawl4ai.extraction_strategy import LLMExtractionStrategy
import asyncio
async def main():
# Basic crawling - get page as markdown
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(url="https://example.com")
print(result.markdown) # clean markdown content
# AI-powered extraction with structured output
strategy = LLMExtractionStrategy(
instruction="Extract all product names and prices from this page",
)
config = CrawlerRunConfig(extraction_strategy=strategy)
async with AsyncWebCrawler() as crawler:
result = await crawler.arun(
url="https://example.com/products",
config=config,
)
print(result.extracted_content) # structured JSON output
asyncio.run(main())
```
```python
# 0. Import
import asyncio
from typing import Awaitable
from gracy import BaseEndpoint, Gracy, GracyConfig, LogEvent, LogLevel
# 1. Define your endpoints
class PokeApiEndpoint(BaseEndpoint):
GET_POKEMON = "/pokemon/{NAME}" # 👈 Put placeholders as needed
# 2. Define your Graceful API
class GracefulPokeAPI(Gracy[str]):
class Config: # type: ignore
BASE_URL = "https://pokeapi.co/api/v2/" # 👈 Optional BASE_URL
# 👇 Define settings to apply for every request
SETTINGS = GracyConfig(
log_request=LogEvent(LogLevel.DEBUG),
log_response=LogEvent(LogLevel.INFO, "{URL} took {ELAPSED}"),
parser={
"default": lambda r: r.json()
}
)
async def get_pokemon(self, name: str) -> Awaitable[dict]:
return await self.get(PokeApiEndpoint.GET_POKEMON, {"NAME": name})
# Note: since Gracy is based on httpx we can customized the used client with custom headers etc"
def _create_client(self) -> httpx.AsyncClient:
client = super()._create_client()
client.headers = {"User-Agent": f"My Scraper"}
return client
pokeapi = GracefulPokeAPI()
async def main():
try:
pokemon = await pokeapi.get_pokemon("pikachu")
print(pokemon)
finally:
pokeapi.report_status("rich")
asyncio.run(main())
```