How to set up and use a proxy in Puppeteer

Puppeteer is a Node.js library that enables JavaScript to control Chromium-based browsers such as Google Chrome, Microsoft Edge, Opera, and Brave. It is particularly useful for automating browser tasks such as navigating pages, interacting with interface elements, generating PDF files, taking screenshots, and performing service tests. One of Puppeteer's key features is its support for headless mode, where the browser operates without a graphical interface. This mode is optimal for web scraping as it significantly enhances the speed of data collection and analysis.
We will next explore how to set up and utilize proxies in Puppeteer, a crucial step to maximize the capabilities of this library. Utilizing proxies is beneficial for several reasons:
- Emulating user behavior: by simulating actions from different devices and IP addresses, it becomes possible to mimic a more natural browsing experience;
- Managing request distribution: proxies can help distribute requests across IPs, which may reduce block rates during high-volume data collection;
- Load balancing: distributing requests across multiple servers can increase scraping speed and efficiency;
- Accessing localized public data: proxies support geo-sensitive use cases such as collecting region-specific content for market research, SERP monitoring, and ad verification.
These advantages underscore the importance of integrating proxy management within Puppeteer setups to ensure successful and efficient web scraping and automation tasks.
Video guide for setting up a proxy in Puppeteer
Step-by-step proxy setup in Puppeteer using JavaScript
To add a proxy to Puppeteer and configure it for use, follow these streamlined steps:
- Launch your development environment, such as Microsoft Visual Studio, and select the JavaScript library.
- Use the following code:
const puppeteer = require('puppeteer'); async function run() { const browser = await puppeteer.launch({ headless: false, args: ['--proxy-server=PROXY_IP:PROXY_PORT'] }); const page = await browser.newPage(); const pageUrl = 'https://example.com/'; // Adding proxy authentication await page.authenticate({ username: 'PROXY_USERNAME', password: 'PROXY_PASSWORD' }); await page.goto(pageUrl); } run(); - The --proxy-server=PROXY_IP:PROXY_PORT argument configures the browser to use the specified proxy.
- --proxy-server: this is a flag used in the command-line argument to indicate that the browser should route its network requests through a proxy server.
- PROXY_IP: replace this with the actual IP address of the proxy server you intend to use.
- PROXY_PORT: substitute this with the port number on which your proxy server is configured to receive connections.
For example, if your proxy is at IP 111.111.11.11 and port 2020, then the code will look like:
args: ['--proxy-server=111.111.11.11 : 2020] - To use a private proxy server, you must provide authentication details. Insert your login credentials into the page.authenticate method. For instance, if your username is myUser and your password is myPass, update the code as follows:
await page.authenticate({ username: 'myUser', password: 'myPass' }); - To specify a start page for the browser, modify the pageUrl variable. Replace the default URL with the one you need. For example, to set the start page to https://example.com/, the code would be:
const pageUrl = 'https://example.com/'; await page.goto(pageUrl);
Using a proxy in Puppeteer to route browser traffic through a specified server can be useful for geo-sensitive data collection, IP masking, and load distribution during web scraping activities.
PROXIES FROM $0.02/IP
Ready to put this into practice?
Pick a proxy type and location, and start using it within minutes.
Instant delivery
24/7 support
Refund policy
About the author

