Javascript 如何在 puppeteer 和 Headless Chrome 中使用代理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/52777757/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use proxy in puppeteer and headless Chrome?
提问by Irina Kazhamiakina
Please tell me how to properly use a proxy with a puppeteer and headless Chrome. My option does not work.
请告诉我如何正确使用带有 puppeteer 和无头 Chrome 的代理。我的选择不起作用。
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();
})();
回答by Chuong Tran
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();
})();
回答by Cuadrix
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.
puppeteer-page-proxy是可能的。它支持为整个页面设置代理,或者如果您愿意,它可以为每个请求设置不同的代理。是的,它适用于无头和有头的 Chrome。
First install it:
首先安装它:
npm i puppeteer-page-proxy
Then require it:
然后要求它:
const useProxy = require('puppeteer-page-proxy');
Using it is easy;Set proxy for an entire page:
使用它很容易;为整个页面设置代理:
await useProxy(page, 'http://127.0.0.1:8000');
If you want a different proxy for each request,then you can simply do this:
如果您想为每个请求使用不同的代理,那么您可以简单地执行以下操作:
await page.setRequestInterception(true);
page.on('request', req => {
    useProxy(req, 'socks5://127.0.0.1:9000');
});
Then if you want to be sure that your page's IP has changed, you can look it up;
然后如果你想确定你的页面的IP有没有变,你可以查一下;
const data = await useProxy.lookup(page);
console.log(data.ip);
It supports http, https, socks4and socks5proxies, and it also supports authentication if that is needed:
它支持http、https、socks4和socks5代理,如果需要,它还支持身份验证:
const proxy = 'http://login:[email protected]:8000'
Repository: https://github.com/Cuadrix/puppeteer-page-proxy
回答by fengyu xiyuan
回答by qrt
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.proxywill 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
否则argv.proxy不会被替换
在你将它传递给启动函数之前检查这个字符串以确保它是正确的,你可能想在该浏览器中访问http://api.ipify.org/以确保代理正常工作
回答by Gajus
You can use https://github.com/gajus/puppeteer-proxyto set proxy either for entire page or for specific requests only, e.g.
您可以使用https://github.com/gajus/puppeteer-proxy为整个页面或仅针对特定请求设置代理,例如
import puppeteer from 'puppeteer';
import {
  createPageProxy,
} from 'puppeteer-proxy';
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  const pageProxy = createPageProxy({
    page,
    proxyUrl: 'http://127.0.0.1:3000',
  });
  await page.setRequestInterception(true);
  page.once('request', async (request) => {
    await pageProxy.proxyRequest(request);
  });
  await page.goto('https://example.com');
})();
To skip proxy simply call request.continue()conditionally.
要跳过代理,只需request.continue()有条件地调用。
Using puppeteer-proxyPagecan have multiple proxies.
使用puppeteer-proxyPage可以有多个代理。

