- Goal: Scrape the first 3 Amazon search results for a given query and return structured product data.
- AI-Powered Search: Uses Stagehand
actto type in the search bar and click search (or optionally navigate directly to the search URL). - Structured Extraction: Uses
extractwith a JSON schema to get product name, price, rating, review count, and product URL. - Model: Uses
google/gemini-2.5-flashfor fast, cost-effective automation. - Docs → https://docs.stagehand.dev
- act: Perform UI actions from a prompt (type in search bar, click search). Docs → https://docs.stagehand.dev/basics/act
- extract: Pull structured data from pages using JSON schemas. Docs → https://docs.stagehand.dev/basics/extract
- cd python/amazon-product-scraping
- cp .env.example .env (or create .env with required keys)
- Add
BROWSERBASE_API_KEYto .env - Optionally edit
SEARCH_QUERYin main.py - Run the script:
uv run python main.py
- Initializes Stagehand session with Browserbase
- Displays live session link for monitoring
- Navigates to Amazon and performs search (or direct URL navigation if uncommented)
- Extracts the first 3 products with name, price, rating, reviews count, and product URL
- Outputs JSON to console
- Closes session cleanly
- Import errors: Ensure you're running with
uv run python main.pyso dependencies are installed - Missing credentials: Verify .env contains
BROWSERBASE_API_KEY - Amazon layout changes: Extraction may need prompt/schema updates if Amazon changes their search results UI
- Find more information on your Browserbase dashboard → https://www.browserbase.com/sign-in
• Price monitoring: Scrape top results for a product query to track prices and availability over time. • Competitor research: Extract product titles, ratings, and review counts for comparison. • Catalog building: Pull structured product data for feeds, dashboards, or internal tools.
• Switch to direct URL: Uncomment the URL-based search block in main.py for faster runs without LLM search actions.
• Parameterize query: Accept SEARCH_QUERY from CLI or env for different products without editing code.
• Paginate: Extend extraction to multiple pages or increase the number of products per run.
📚 Stagehand Docs: https://docs.stagehand.dev 📚 Python SDK: https://docs.stagehand.dev/v3/sdk/python 🎮 Browserbase: https://www.browserbase.com 💡 Try it out: https://www.browserbase.com/playground 🔧 Templates: https://www.browserbase.com/templates 📧 Need help? support@browserbase.com 💬 Discord: http://stagehand.dev/discord