What is the primary purpose of using a proxy in Puppeteer and headless Chrome?
To use a proxy in Puppeteer with the puppeteer-page-proxy library:
-
Install the library with npm:
npm i puppeteer-page-proxy
-
Require the library in your code:
const useProxy = require('puppeteer-page-proxy');
-
Set a proxy for the entire page:
await useProxy(page, 'http://127.0.0.1:8000');
-
If you want a different proxy for each request, enable request interception and set proxies for requests:
await page.setRequestInterception(true); page.on('request', req => { useProxy(req, 'socks5://127.0.0.1:9000'); });
-
To confirm the changed IP address of the page:
const data = await useProxy.lookup(page); console.log(data.ip);
The library supports various proxy types (http, https, socks4, and socks5) and authentication if needed.
Repository: https://github.com/Cuadrix/puppeteer-page-proxy