Skip to main content
Below are examples of Browser API usage in various scenarios and libraries.
Please make sure to install required libraries before continuing

Make your first request in minutes

Test the Browser API in minutes with these ready-to-use code examples.
Simple scraping of targeted page

Select your preferred tech-stack

#!/usr/bin/env node
const playwright = require('playwright');
const {
    AUTH = 'SBR_ZONE_FULL_USERNAME:SBR_ZONE_PASSWORD',
    TARGET_URL = 'https://example.com',
} = process.env;

async function scrape(url = TARGET_URL) {
    if (AUTH == 'SBR_ZONE_FULL_USERNAME:SBR_ZONE_PASSWORD') {
        throw new Error(`Provide Browser API credentials in AUTH`
            + ` environment variable or update the script.`);
    }
    console.log(`Connecting to Browser...`);
    const endpointURL = `wss://${AUTH}@brd.superproxy.io:9222`;
    const browser = await playwright.chromium.connectOverCDP(endpointURL);
    try {
        console.log(`Connected! Navigating to ${url}...`);
        const page = await browser.newPage();
        await page.goto(url, { timeout: 2 * 60 * 1000 });
        console.log(`Navigated! Scraping page content...`);
        const data = await page.content();
        console.log(`Scraped! Data: ${data}`);
    } finally {
        await browser.close();
    }
}

if (require.main == module) {
    scrape().catch(error => {
        console.error(error.stack || error.message || error);
        process.exit(1);
    });
}

Optimizing Bandwidth Usage with Browser API

When optimizing your web scraping projects, conserving bandwidth is key. Explore our tips and guidelines below to utilize bandwidth-saving techniques within your script and ensure efficient, resource-friendly scraping.

Avoid Unnecessary Media Content

Downloading unnecessary media (images, videos) is a common bandwidth drain. You can block these resources directly within your script.
Resource-blocking can occasionally impact page loading due to anti-bot expectations. If you see issues after blocking resources, revert your blocking logic before contacting support.
 const page = await browser.newPage();  
  
  // Enable request interception  
  await page.setRequestInterception(true);  
  
  // Listen for requests  
  page.on('request', (request) => {  
    if (request.resourceType() === 'image') {  
      // If the request is for an image, block it  
      request.abort();  
    } else {  
      // If it's not an image request, allow it to continue  
      request.continue();  
   }  
 });

Block Unnecessary Network Requests

Blocking media type requests alone may not always reduce your bandwidth usage. Some websites have ad spaces that continuously refresh ads, and others use live bidding mechanisms that constantly search for new ads if one fails to load properly. In such cases, it’s important to identify and block these specific network requests. Doing so will decrease the number of network requests and, consequently, lower your bandwidth usage.
Example
 const blocked_resources = [
     "image",
     "stylesheet",
     "font",
     "media",
     "svg"
 ];
 
 const blocked_urls = [
     'www.googletagmanager.com/gtm.js',
     'cdn.adapex.io/hb',
     'pagead2.googlesyndication.com/',
 ];
 
 await page.setRequestInterception(true);
 
 page.on('request', request => {
     counter++;
     const is_url_blocked = blocked_urls.some(p => request.url().includes(p));
     const is_resource_blocked = blocked_resources.includes(request.resourceType());
     if (is_url_blocked || is_resource_blocked) {
         request.abort();
     } else {
         request.continue();
     }
 });

Use Browser Cache Efficiently

One common inefficiency in scraping jobs is the repeated downloading of the same resources during a single session. Leveraging the browser’s built-in cache can significantly increase your scraping efficiency. When you navigate to multiple pages on the same domain, static resources like CSS files, JavaScript libraries, images, and fonts are automatically cached and reused. This saves bandwidth by avoiding redundant fetches and ensures faster page loads for subsequent requests.

Code Example

This example demonstrates how browser caching improves performance across multiple navigations to the same domain. Make sure to replace USER:PASS with your own credentials
Puppeteer
#!/usr/bin/env node
const puppeteer = require('puppeteer-core');

const AUTH = process.env.AUTH || 'USER:PASS';
const SBR_WS_ENDPOINT = `wss://${AUTH}@brd.superproxy.io:9222`;
const URL = 'https://www.ebay.com';

async function main() {
    console.log('Connecting to Scraping Browser...');
    const browser = await puppeteer.connect({
        browserWSEndpoint: SBR_WS_ENDPOINT,
    });
    
    try {
        const page = await browser.newPage();
        page.setDefaultTimeout(2 * 60 * 1000);
        
        const searchTerms = ['laptop', 'headphones', 'keyboard'];
        const timings = [];
        
        for (let i = 0; i < 3; i++) {
            console.log(`Running search ${i + 1}/3 for "${searchTerms[i]}"...`);
            
            const start = Date.now();
            await page.goto(URL, { waitUntil: "domcontentloaded" });
            const navTime = Date.now() - start;
            timings.push(navTime);
            console.log(`Search ${i + 1} nav time is ${navTime}ms`);
            
            // Wait for search input and perform search
            await page.waitForSelector("#gh-ac", { timeout: 30000 });
            await page.type("#gh-ac", searchTerms[i]);
            await page.click("#gh-search-btn");
            
            // Wait for results
            await page.waitForSelector("#srp-river-main", { timeout: 30000 });
        }
        
        console.log('\nResults:');
        const diff1to2 = timings[1] - timings[0];
        const diff1to3 = timings[2] - timings[0];

        console.log(
		`1st: ${timings[0]}ms | `+
        `2nd: ${timings[1]}ms (${diff1to2 > 0 ? '+' : ''}${diff1to2}ms) | `+
        `3rd: ${timings[2]}ms (${diff1to3 > 0 ? '+' : ''}${diff1to3}ms)`);
        
    } catch (error) {
        console.error(`ERROR: ${error.message}`);
        process.exit(1);
    } finally {
        await browser.close();
    }
}

main().catch(error => {
    console.error(`ERROR: ${error.message}`);
    process.exit(1);
});

Expected Output

Connecting to Scraping Browser...
Running search 1/3 for "laptop"...
Search 1 nav time is 10417ms
Running search 2/3 for "headphones"...
Search 2 nav time is 3133ms
Running search 3/3 for "keyboard"...
Search 3 nav time is 3243ms

Results:
1st: 10417ms | 2nd: 3133ms (-7284ms) | 3rd: 3243ms (-7174ms)

Other Strategies

  • Limit Your Requests: Scrape only the data you need.
  • Concurrency Control: Avoid opening too many concurrent pages; this can overload resources.
  • Session Management: Properly close sessions to save resources and bandwidth.
  • Opt for APIs: Use official APIs when available—they’re often less bandwidth-intense.
  • Fetch Incremental Data: Only scrape new/updated content, not the entire dataset every time.