GBProxies

如何在Puppeteer中使用認證代理

Chrome將代理主機和端口作為啟動標誌,Puppeteer使用page.authenticate回答登錄提示。

步驟

  1. 使用--proxy-server=host:port啟動Chrome
  2. 使用您的代理用戶名和密碼調用page.authenticate
  3. 開啟請求攔截以阻止圖片和字體
  4. 為每個會話ID打開一個瀏覽器以保持固定IP

示例代碼

import puppeteer from "puppeteer";

const browser = await puppeteer.launch({ args: ["--proxy-server=gw.gbproxies.cc:10000"] });
const page = await browser.newPage();
await page.authenticate({ username: "USERNAME-cc-fr-sid-shop1-ttl-30", password: "PASSWORD" });
await page.setRequestInterception(true);
page.on("request", (r) => (["image", "font", "media"].includes(r.resourceType()) ? r.abort() : r.continue()));
await page.goto("https://api.ipify.org");
console.log(await page.content());
await browser.close();

將USERNAME和PASSWORD替換為控制台Proxies頁面上的proxy登錄信息。端口10000是住宅HTTP;請查看下方每個產品的端口。

提示

  • 阻止圖片通常可以將每頁流量減少一半以上
  • 固定會話ID會為整個瀏覽流程保持相同的IP

常見問題

為什麼Chrome仍然顯示登錄彈出窗口?

page.authenticate必須在該頁面的第一次導航之前運行。

如何在Puppeteer中使用認證代理