How to use proxy in puppeteer and headless Chrome?

假装没事ソ 提交于 2020-01-01 02:47:08

问题


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();
})();

回答1:


You can find an example about proxy at here

'use strict';

const puppeteer = require('puppeteer');

(async() => {
  const browser = await puppeteer.launch({
    // Launch chromium using a proxy server on port 9876.
    // More on proxying:
    //    https://www.chromium.org/developers/design-documents/network-settings
    args: [ '--proxy-server=127.0.0.1:9876' ]
  });
  const page = await browser.newPage();
  await page.goto('https://google.com');
  await browser.close();
})();



回答2:


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




回答3:


do not use

"--proxy-server =${argv.proxy}"  

this is a normal string instead of template literal
use ` instead of "

`--proxy-server =${argv.proxy}`

otherwise argv.proxy will not be replaced
check 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



来源:https://stackoverflow.com/questions/52777757/how-to-use-proxy-in-puppeteer-and-headless-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!