Puppeteer Proxy Rotation in Node.js: Setup, Auth, and Residential Proxies
Introduction
Web scraping and automation tasks often require rotating proxies to maintain anonymity, bypass IP blacklisting, or access geo-restricted content. Node.js with Puppeteer is a popular choice for such tasks due to its flexibility and ease of use. In this guide, we'll explore how to set up proxy rotation in Puppeteer using both --proxy-server arguments and SOCKS5 proxies with the socks-proxy-agent. We'll also cover handling authentication with page.authenticate() and discuss strategies for rotating proxies on a per-request basis.
Setting Up Basic Proxy Rotation
Puppeteer can launch browsers with a specified proxy server. This is straightforward and doesn't require any additional libraries.
const puppeteer = require('puppeteer');
async function launchWithProxy(proxyUrl) {
const browser = await puppeteer.launch({
args: [`--proxy-server=${proxyUrl}`]
});
const page = await browser.newPage();
await page.goto('https://httpbin.org/ip');
console.log(await page.content());
await browser.close();
}
launchWithProxy('http://your-proxy.com:8080').catch(console.error);
Note: This method is suitable for HTTP and HTTPS proxies. For SOCKS5, you need to use the socks-proxy-agent library.
Using SOCKS5 Proxies
For more complex proxy setups or when using residential proxies, SOCKS5 is often required. The socks-proxy-agent package allows Puppeteer to work with SOCKS5 proxies.
First, install the necessary packages:
npm install puppeteer socks-proxy-agent
Then, use the following code snippet:
const puppeteer = require('puppeteer');
const SocksProxyAgent = require('socks-proxy-agent');
async function launchWithSocksProxy(proxyUrl) {
const agent = new SocksProxyAgent(proxyUrl);
const browser = await puppeteer.launch({
args: [`--proxy-server=${proxyUrl}`]
});
const page = await browser.newPage();
await page._client.send('Network.setExtraHTTPHeaders', {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'
});
await page.authenticate({username: 'your-username', password: 'your-password'});
await page.setExtraHTTPHeaders({'Proxy-Authorization': `Basic ${Buffer.from('your-username:your-password').toString('base64')}`});
await page.goto('https://httpbin.org/ip');
console.log(await page.content());
await browser.close();
}
launchWithSocksProxy('socks5h://user:[email protected]:1080').catch(console.error);
Proxy Rotation Strategy
To rotate proxies per request, maintain a pool of proxy URLs and cycle through them:
const puppeteer = require('puppeteer');
const SocksProxyAgent = require('socks-proxy-agent');
const proxyPool = [
'socks5h://user1:[email protected]:1080',
'socks5h://user2:[email protected]:1080'
];
let currentProxyIndex = 0;
async function launchWithRotatingProxy() {
const proxyUrl = proxyPool[currentProxyIndex];
const agent = new SocksProxyAgent(proxyUrl);
const browser = await puppeteer.launch({
args: [`--proxy-server=${proxyUrl}`]
});
const page = await browser.newPage();
// Authenticate and set extra headers as needed
await page.authenticate({username: 'your-username', password: 'your-password'});
await page.goto('https://httpbin.org/ip');
console.log(await page.content());
await browser.close();
// Rotate the proxy index for the next request
currentProxyIndex = (currentProxyIndex + 1) % proxyPool.length;
}
launchWithRotatingProxy().catch(console.error);
Free Proxy Lists vs. Residential Proxies
Free proxy lists can be unreliable and are often blocked by websites due to their lack of stability and high volume of users. They may also have restrictions on bandwidth, usage speed, and IP blacklisting.
In contrast, residential proxies like those from Bright Data or SmartProxy provide more stable connections as they mimic real user traffic. They offer better anonymity, higher speeds, and fewer restrictions.
Comparison with Playwright
If you're considering using Playwright instead of Puppeteer, the proxy setup process is similar but uses different methods:
- Playwright Proxy Setup:
const { chromium } = require('playwright');
async function launchWithProxy(proxyUrl) {
const browser = await chromium.launch({
args: [`--proxy-server=${proxyUrl}`]
});
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://httpbin.org/ip');
console.log(await page.content());
await browser.close();
}
launchWithProxy('http://your-proxy.com:8080').catch(console.error);
Playwright also supports SOCKS5 proxies and has a similar authentication method.
Conclusion
Implementing proxy rotation in Puppeteer or Playwright can significantly enhance the reliability of your web scraping and automation tasks. By using both --proxy-server arguments and socks-proxy-agent, you can handle various proxy types effectively. Remember to use authenticated proxies for residential services to ensure smooth operations.
For developers looking to automate and manage proxy lists without manual intervention, ProxyForge offers a solution. It auto-tests and rotates proxies locally on your machine with a one-off purchase model, ensuring reliable and secure connections for your projects: https://proxyforge.dev/#download.
Free proxies break. ProxyForge doesn't.
The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.
Download ProxyForge free →