Please tell me how to properly use a proxy with a puppeteer and headless Chrome. My option does not work.
const puppeteer = require('puppeteer');
(async () => {
const argv = require('minimist')(process.argv.slice(2));
const browser = await puppeteer.launch({args: ["--proxy-server =${argv.proxy}","--no-sandbox", "--disable-setuid-sandbox"]});
const page = await browser.newPage();
await page.setJavaScriptEnabled(false);
await page.setUserAgent(argv.agent);
await page.setDefaultNavigationTimeout(20000);
try{
await page.goto(argv.page);
const bodyHTML = await page.evaluate(() => new XMLSerializer().serializeToString(document))
body = bodyHTML.replace(/\r|\n/g, '');
console.log(body);
}catch(e){
console.log(e);
}
await browser.close();
})();
You can find an example about proxy at here
if you want to use different proxy for per page, try this, use https-proxy-agent or http-proxy-agent to proxy request for per page
You can use https://github.com/gajus/puppeteer-proxy to set proxy either for entire page or for specific requests only, e.g.
To skip proxy simply call
request.continue()
conditionally.Using puppeteer-proxy
Page
can have multiple proxies.It's possible with puppeteer-page-proxy. It supports setting a proxy for an entire page, or if you like, it can set a different proxy for each request. And yes, it works both in headless and headful Chrome.
First install it:
Then require it:
Using it is easy; Set proxy for an entire page:
If you want a different proxy for each request,then you can simply do this:
Then if you want to be sure that your page's IP has changed, you can look it up;
It supports http, https, socks4 and socks5 proxies, and it also supports authentication if that is needed:
Repository: https://github.com/Cuadrix/puppeteer-page-proxy
do not use
this is a normal string instead of template literal
use ` instead of "
otherwise
argv.proxy
will not be replacedcheck this string before you pass it to launch function to make sure it's correct and you may want to visit http://api.ipify.org/ in that browser to make sure the proxy works normally