-
-
Save nibzard/6a2f3911a1953b7bef363a411c45a95b to your computer and use it in GitHub Desktop.
Steel happy path appendix script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require('dotenv').config(); | |
| const Steel = require('steel-sdk'); | |
| const puppeteer = require('puppeteer-core'); | |
| // Initialize Steel with your API key | |
| const steel = new Steel({ steelAPIKey: process.env.STEEL_API_KEY }); | |
| async function scrapeExample() { | |
| // Step 1: Request a browser session from Steel | |
| console.log('Starting a Steel browser session...'); | |
| const session = await steel.sessions.create(); | |
| console.log(`Session created: ${session.id}`); | |
| // Step 2: Connect Puppeteer to your cloud browser | |
| console.log('Connecting to the browser...'); | |
| const browser = await puppeteer.connect({ | |
| browserWSEndpoint: `wss://connect.steel.dev?apiKey=${process.env.STEEL_API_KEY}&sessionId=${session.id}` | |
| }); | |
| // Step 3: Open a new page and navigate | |
| console.log('Navigating to example.com...'); | |
| const page = await browser.newPage(); | |
| await page.goto('https://example.com'); | |
| console.log('Page loaded!'); | |
| // Step 4: Extract the information we want | |
| console.log('Reading page content...'); | |
| const pageData = await page.evaluate(() => { | |
| return { | |
| h1: document.querySelector('h1')?.textContent, | |
| paragraph: document.querySelector('p')?.textContent, | |
| allLinks: Array.from(document.querySelectorAll('a')).map(a => ({ | |
| text: a.textContent, | |
| href: a.href | |
| })) | |
| }; | |
| }); | |
| console.log('Data extracted:'); | |
| console.log(JSON.stringify(pageData, null, 2)); | |
| // Step 5: Clean up - close everything | |
| console.log('Closing the browser and session...'); | |
| await browser.close(); | |
| await steel.sessions.release(session.id); | |
| console.log('Done! Session closed.'); | |
| return pageData; | |
| } | |
| // Run the automation | |
| scrapeExample() | |
| .then(data => { | |
| console.log('\nSuccess! Here\'s what we got:'); | |
| console.log(data); | |
| }) | |
| .catch(error => { | |
| console.error('Something went wrong:', error.message); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment