docs/examples/c4a_script/amazon_example/README.md
A real-world demonstration of Crawl4AI's multi-step crawling with LLM-generated automation scripts.
This example demonstrates advanced Crawl4AI features:
The example uses C4ACompiler.generate_script() to analyze Amazon's HTML and create:
Homepage → Execute Search Script → Extract Products → Save Results
All steps use the same session_id to maintain browser state.
Products are extracted with:
amazon_r2d2_search.py - Main example scriptheader.html - Amazon search bar HTML (provided)product.html - Product card HTML (provided)generated_search_script.c4a - Auto-generated search automationgenerated_product_schema.json - Auto-generated extraction rulesextracted_products.json - Final scraped datasearch_results_screenshot.png - Visual proof of resultsPrerequisites
# Ensure Crawl4AI is installed
pip install crawl4ai
# Set up LLM API key (for script generation)
export OPENAI_API_KEY="your-key-here"
Run the scraper
python amazon_r2d2_search.py
Watch the magic!
[
{
"title": "Death Star BB8 R2D2 Golf Balls with 20 Printed tees",
"price": "29.95",
"rating": "4.7",
"reviews_count": "184",
"delivery": "FREE delivery Thu, Jun 19",
"url": "https://www.amazon.com/Death-Star-R2D2-Balls-Printed/dp/B081XSYZMS",
"is_sponsored": true,
"small_business": true
},
...
]
# Same session_id across multiple arun() calls
config = CrawlerRunConfig(
session_id="amazon_r2d2_session",
# ... other settings
)
# Generate automation from natural language + HTML
script = C4ACompiler.generate_script(
html=header_html,
query="Find search box, type 'r2d2', click search",
mode="c4a"
)
# Structured data extraction with CSS selectors
schema = {
"baseSelector": "[data-component-type='s-search-result']",
"fields": [
{"name": "title", "selector": "h2 a span", "type": "text"},
{"name": "price", "selector": ".a-price-whole", "type": "text"}
]
}
Change the search term in the script generation:
search_goal = """
...
3. Type "star wars lego" into the search box
...
"""
Add fields to the extraction schema:
"fields": [
# ... existing fields
{"name": "prime", "selector": ".s-prime", "type": "exists"},
{"name": "image_url", "selector": "img.s-image", "type": "attribute", "attribute": "src"}
]
Adapt the approach for other e-commerce sites by:
This example shows the power of combining LLM intelligence with web automation. The scripts adapt to HTML changes and natural language instructions make automation accessible to everyone!