DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Overcoming IP Bans During Web Scraping with TypeScript Under Tight Deadlines

In the fast-paced environment of web scraping, encountering an IP ban can halt progress and introduce significant setbacks, especially when working under tight deadlines. As a Senior Architect, leveraging experience to implement robust, scalable, and quick solutions is paramount. In this article, I will share proven strategies and TypeScript implementations to circumvent IP bans effectively while maintaining ethical scraping practices.

Understanding the Challenge

Websites deploy various anti-scraping measures, including IP rate limiting, connection throttling, and outright bans. When scraping large volumes of data, your IP address becomes a focal point for detection and restriction. A common but quick fix is to rotate IPs or use proxy services, but these solutions require careful integration to avoid detection.

Step 1: Implementing a Reliable Proxy Pool

The first step is to create a diversified proxy pool. Free proxies are unreliable, so leverage reputable paid proxy services that offer multiple IPs across different regions. To prevent IP blocks, dynamically switch proxies after every few requests or upon detecting a ban.

Here's an example of how to manage proxies efficiently in TypeScript:

interface Proxy {
  ip: string;
  port: number;
  protocol: 'http' | 'https' | 'socks5';
}

const proxyPool: Proxy[] = [
  { ip: '192.168.1.1', port: 8080, protocol: 'http' },
  { ip: '192.168.1.2', port: 8080, protocol: 'http' },
  // Add more proxies
];

function getRandomProxy(): Proxy {
  const index = Math.floor(Math.random() * proxyPool.length);
  return proxyPool[index];
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Managing Request Headers with Randomization

To mimic human behavior and evade detection, randomize headers such as User-Agent, Accept-Language, and Referer. This strategy limits fingerprinting based on typical request patterns.

const userAgents = [
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64)...',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...',
  // More user agents
];

function getRandomUserAgent(): string {
  const index = Math.floor(Math.random() * userAgents.length);
  return userAgents[index];
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Implementing Retry and Blacklist Detection

When a request gets blocked, the response status or content often indicates a ban. Incorporate logic to detect such flags and react accordingly by switching proxies and throttling requests.

import axios from 'axios';

async function fetchWithRetry(url: string): Promise<string> {
  let attempts = 0;
  const maxRetries = 3;
  while (attempts < maxRetries) {
    const proxy = getRandomProxy();
    try {
      const response = await axios.get(url, {
        proxy: {
          host: proxy.ip,
          port: proxy.port,
        },
        headers: {
          'User-Agent': getRandomUserAgent(),
          'Accept-Language': 'en-US'
        },
        timeout: 5000
      });
      if (response.status === 200 && !isBannedResponse(response.data)) {
        return response.data;
      }
    } catch (error) {
      // Log error and proceed to switch proxy
    }
    attempts++;
    // Short delay before retrying
    await new Promise(res => setTimeout(res, 2000));
  }
  throw new Error('Max retries reached or IP banned');
}

function isBannedResponse(data: string): boolean {
  // Logic to detect ban in response content
  return data.includes('captcha') || data.includes('access denied');
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Using Residential Proxies and Headless Browsers

For high-stakes scraping, resort to residential proxies or headless browsers like Puppeteer. These emulate real-user interactions, significantly reducing ban risks.

import puppeteer from 'puppeteer';

async function scrapeWithPuppeteer(url: string) {
  const browser = await puppeteer.launch({ args: ['--proxy-server=proxy:port'] });
  const page = await browser.newPage();
  await page.setUserAgent(getRandomUserAgent());
  await page.goto(url, { waitUntil: 'networkidle2' });
  const content = await page.content();
  await browser.close();
  return content;
}
Enter fullscreen mode Exit fullscreen mode

Final Notes

Combining proxy rotation, header randomization, request throttling, and headless browsing creates a multi-layered defense against IP bans. Always adhere to legal and ethical guidelines when scraping and respect target websites' robots.txt policies. Under pressing deadlines, integrate these strategies incrementally, monitor their effectiveness, and scale dynamically.

By leveraging these techniques efficiently, you can maintain the continuity of your data collection workflows without compromising system integrity or facing bans again. Remember, in long-term projects, building reputation and minimizing impact are key to sustainable scraping practices.


🛠️ QA Tip

Pro Tip: Use TempoMail USA for generating disposable test accounts.

Top comments (0)