Stock Price Scraper
This first extractor shows the "Playwright way" of scraping: open a browser, navigate to a page, grab a value, and return the data.
Creating this extractor is a simple 3-step process:
1. Write a Playwright scraper function
2. Add the function to the server
3. Create a simple text input for the user interface
Follow along and you'll have a desktop app fetching real-time stock prices in under 10 minutes.
Step 1: Create the Scraper Functionβ
Open src/scraper/src/scraper.ts
and replace all its contents with the code below. This creates our stockPriceScraper
function that will:
- Receive a stock symbol from user input
- Build the Yahoo Finance URL for that specific stock
- Navigate to the page using Playwright
- Extract the current stock price from this specific element:
- Return the data
import { playwright } from 'botasaurus/playwright';
/**
* Grabs the live quote for a given stock symbol from Yahoo Finance.
*/
const stockPriceScraper = playwright({
// π MUST match the input file name we'll create in Step 3
name: 'stockPriceScraper',
headless: false, // Show the browser window while scraping
reuseDriver: true, // Reuse the same browser instance for multiple tasks
run: async ({ data, page }) => {
// Extract the stock symbol from user input
const stock_symbol = data['stock_symbol'];
// Build the Yahoo Finance URL
const link = `https://finance.yahoo.com/quote/${stock_symbol}`;
// Navigate to the Yahoo Finance page
await page.goto(link, { waitUntil: 'domcontentloaded' });
// Extract the stock price using a selector
const stock_price = parseFloat(
(await page.textContent('[data-testid="qsp-price"]')) as string,
);
// Return the data
return {
stock_symbol: stock_symbol,
stock_price: stock_price,
};
},
});
export { stockPriceScraper };
Have you noticed we named our scraper stockPriceScraper
?
This name is crucialβBotasaurus uses it to connect the scraper with its input controls js file, which we'll create in Step 3.
Step 2: Add the Scraperβ
Now let's connect our scraper function to the desktop app. Open src/scraper/backend/server.ts
and replace all its contents with the code below:
import { Server } from 'botasaurus-server/server';
import { stockPriceScraper } from '../src/stockPriceScraper';
Server.addScraper(stockPriceScraper);
This single line connects your scraper with the desktop app, making it available in the user interface.
Step 3: Create the Input Controlsβ
Finally, let's create the user interface. Create a new file named stockPriceScraper.js
in the inputs
folder:
/**
* @typedef {import('botasaurus-controls').Controls} Controls // π Enables Code autocomplete
*/
/**
* Renders the form users see on the Home page.
* @param {Controls} controls
*/
function getInput(controls) {
controls.text("stock_symbol", {
label: "Stock Symbol",
placeholder: "e.g. AAPL",
defaultValue: "AAPL",
isRequired: true,
validate: (value) => {
const errors = [];
if (value.length !== 4) {
errors.push("Length must be 4 characters");
}
if (!/^[A-Z]+$/.test(value)) {
errors.push("Only uppercase letters allowed");
}
return errors;
},
});
}
This code creates a text input field where users can enter stock symbols.
Also, the JSDoc comments enable code completion in VSCode, helping you discover all available input controls.
π You're Done!β
Congratulations! You've just built a fully functional Stock Price Scraper that runs as a desktop application.
To test it:
- Press
Ctrl/β + R
to reload the app - Type any stock symbol (or use the default "AAPL") and click Run
- Watch as Chrome opens Yahoo Finance and navigates to your stock
- Click on the app window to see your results in a table format
The next section will show you how to build an Amazon Invoice extractor that can save accountants hours of manual work.