|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +# Advanced SmartScraper Example |
| 5 | +# |
| 6 | +# This example demonstrates advanced features of SmartScraper including: |
| 7 | +# - Custom JSON schema for structured output |
| 8 | +# - Pagination handling |
| 9 | +# - Infinite scrolling |
| 10 | +# - Custom headers and cookies |
| 11 | +# - Heavy JavaScript rendering |
| 12 | +# - Website interaction steps |
| 13 | + |
| 14 | +require "bundler/setup" |
| 15 | +require "scrapegraphai" |
| 16 | + |
| 17 | +# Initialize the client |
| 18 | +client = Scrapegraphai::Client.new( |
| 19 | + api_key: ENV["SCRAPEGRAPHAI_API_KEY"] |
| 20 | +) |
| 21 | + |
| 22 | +puts "🚀 Advanced SmartScraper Example" |
| 23 | +puts "=" * 50 |
| 24 | + |
| 25 | +begin |
| 26 | + # Example 1: Using custom JSON schema for structured output |
| 27 | + puts "\n📋 Using custom JSON schema..." |
| 28 | + |
| 29 | + custom_schema = { |
| 30 | + type: "object", |
| 31 | + properties: { |
| 32 | + products: { |
| 33 | + type: "array", |
| 34 | + items: { |
| 35 | + type: "object", |
| 36 | + properties: { |
| 37 | + name: { type: "string" }, |
| 38 | + price: { type: "string" }, |
| 39 | + availability: { type: "string" }, |
| 40 | + rating: { type: "number" }, |
| 41 | + description: { type: "string" } |
| 42 | + }, |
| 43 | + required: ["name", "price"] |
| 44 | + } |
| 45 | + }, |
| 46 | + total_products: { type: "integer" }, |
| 47 | + page_info: { |
| 48 | + type: "object", |
| 49 | + properties: { |
| 50 | + current_page: { type: "integer" }, |
| 51 | + has_next_page: { type: "boolean" } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + result = client.smartscraper.create( |
| 58 | + user_prompt: "Extract all products with their details", |
| 59 | + website_url: "https://books.toscrape.com/", |
| 60 | + output_schema: custom_schema |
| 61 | + ) |
| 62 | + |
| 63 | + puts "Request ID: #{result.request_id}" |
| 64 | + puts "Status: #{result.status}" |
| 65 | + puts "Structured Data:" |
| 66 | + puts JSON.pretty_generate(result.result) if result.result |
| 67 | + |
| 68 | + # Example 2: Handling pagination |
| 69 | + puts "\n📄 Handling pagination..." |
| 70 | + |
| 71 | + result = client.smartscraper.create( |
| 72 | + user_prompt: "Extract all book titles and prices from multiple pages", |
| 73 | + website_url: "https://books.toscrape.com/", |
| 74 | + total_pages: 3 # Process first 3 pages |
| 75 | + ) |
| 76 | + |
| 77 | + puts "Request ID: #{result.request_id}" |
| 78 | + puts "Status: #{result.status}" |
| 79 | + puts "Paginated Data:" |
| 80 | + puts JSON.pretty_generate(result.result) if result.result |
| 81 | + |
| 82 | + # Example 3: Using custom headers and cookies |
| 83 | + puts "\n🍪 Using custom headers and cookies..." |
| 84 | + |
| 85 | + custom_headers = { |
| 86 | + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36", |
| 87 | + "Accept-Language": "en-US,en;q=0.9" |
| 88 | + } |
| 89 | + |
| 90 | + custom_cookies = { |
| 91 | + "session_id": "abc123", |
| 92 | + "preferences": "theme=dark" |
| 93 | + } |
| 94 | + |
| 95 | + result = client.smartscraper.create( |
| 96 | + user_prompt: "Extract main content and any personalization elements", |
| 97 | + website_url: "https://httpbin.org/headers", |
| 98 | + headers: custom_headers, |
| 99 | + cookies: custom_cookies |
| 100 | + ) |
| 101 | + |
| 102 | + puts "Request ID: #{result.request_id}" |
| 103 | + puts "Status: #{result.status}" |
| 104 | + puts "Data with Custom Headers/Cookies:" |
| 105 | + puts JSON.pretty_generate(result.result) if result.result |
| 106 | + |
| 107 | + # Example 4: Infinite scrolling |
| 108 | + puts "\n🔄 Handling infinite scroll..." |
| 109 | + |
| 110 | + result = client.smartscraper.create( |
| 111 | + user_prompt: "Extract all visible items after scrolling", |
| 112 | + website_url: "https://quotes.toscrape.com/scroll", |
| 113 | + number_of_scrolls: 3 # Perform 3 scroll operations |
| 114 | + ) |
| 115 | + |
| 116 | + puts "Request ID: #{result.request_id}" |
| 117 | + puts "Status: #{result.status}" |
| 118 | + puts "Scrolled Data:" |
| 119 | + puts JSON.pretty_generate(result.result) if result.result |
| 120 | + |
| 121 | + # Example 5: Heavy JavaScript rendering |
| 122 | + puts "\n⚡ Heavy JavaScript rendering..." |
| 123 | + |
| 124 | + result = client.smartscraper.create( |
| 125 | + user_prompt: "Extract dynamically loaded content", |
| 126 | + website_url: "https://quotes.toscrape.com/js/", |
| 127 | + render_heavy_js: true # Enable heavy JS rendering |
| 128 | + ) |
| 129 | + |
| 130 | + puts "Request ID: #{result.request_id}" |
| 131 | + puts "Status: #{result.status}" |
| 132 | + puts "JS-Rendered Data:" |
| 133 | + puts JSON.pretty_generate(result.result) if result.result |
| 134 | + |
| 135 | + # Example 6: Website interaction steps |
| 136 | + puts "\n🖱️ Website interactions..." |
| 137 | + |
| 138 | + interaction_steps = [ |
| 139 | + "click on button with text 'Load More'", |
| 140 | + "wait for 2 seconds", |
| 141 | + "click on tab 'Popular Quotes'" |
| 142 | + ] |
| 143 | + |
| 144 | + result = client.smartscraper.create( |
| 145 | + user_prompt: "Extract content after performing interactions", |
| 146 | + website_url: "https://quotes.toscrape.com/", |
| 147 | + steps: interaction_steps |
| 148 | + ) |
| 149 | + |
| 150 | + puts "Request ID: #{result.request_id}" |
| 151 | + puts "Status: #{result.status}" |
| 152 | + puts "Interactive Data:" |
| 153 | + puts JSON.pretty_generate(result.result) if result.result |
| 154 | + |
| 155 | + # Example 7: Processing HTML content directly |
| 156 | + puts "\n📄 Processing HTML content directly..." |
| 157 | + |
| 158 | + html_content = <<~HTML |
| 159 | + <!DOCTYPE html> |
| 160 | + <html> |
| 161 | + <head><title>Sample Page</title></head> |
| 162 | + <body> |
| 163 | + <div class="product"> |
| 164 | + <h1>Sample Product</h1> |
| 165 | + <p class="price">$29.99</p> |
| 166 | + <p class="description">This is a sample product description.</p> |
| 167 | + <span class="stock">In Stock</span> |
| 168 | + </div> |
| 169 | + <div class="product"> |
| 170 | + <h1>Another Product</h1> |
| 171 | + <p class="price">$19.99</p> |
| 172 | + <p class="description">Another sample product.</p> |
| 173 | + <span class="stock">Out of Stock</span> |
| 174 | + </div> |
| 175 | + </body> |
| 176 | + </html> |
| 177 | + HTML |
| 178 | + |
| 179 | + result = client.smartscraper.create( |
| 180 | + user_prompt: "Extract all products with their name, price, description, and stock status", |
| 181 | + website_html: html_content # Process HTML directly instead of URL |
| 182 | + ) |
| 183 | + |
| 184 | + puts "Request ID: #{result.request_id}" |
| 185 | + puts "Status: #{result.status}" |
| 186 | + puts "HTML-Processed Data:" |
| 187 | + puts JSON.pretty_generate(result.result) if result.result |
| 188 | + |
| 189 | +rescue Scrapegraphai::Errors::UnprocessableEntityError => e |
| 190 | + puts "❌ Invalid request parameters. Please check your input." |
| 191 | + puts "Error: #{e.message}" |
| 192 | +rescue Scrapegraphai::Errors::APIError => e |
| 193 | + puts "❌ API Error occurred:" |
| 194 | + puts "Status: #{e.status}" if e.respond_to?(:status) |
| 195 | + puts "Error: #{e.message}" |
| 196 | +rescue => e |
| 197 | + puts "❌ Unexpected error occurred:" |
| 198 | + puts "Error: #{e.message}" |
| 199 | +end |
| 200 | + |
| 201 | +puts "\n✅ Advanced SmartScraper example completed!" |
0 commit comments