Node.js Puppeteer 隧道代理:Chrome 浏览器自动化集成爬虫代理
Puppeteer 配置亿牛云爬虫代理,通过 --proxy-server 启动参数和 page.authenticate 处理代理认证。
亿牛云技术团队2026年5月25日2 分钟阅读
Puppeteer + 爬虫代理
Puppeteer 是 Google 维护的 Chrome 浏览器自动化库。与 Playwright 不同,Puppeteer 通过 Chrome 启动参数 --proxy-server 配置代理,通过 page.authenticate() 处理代理认证。
环境准备
export PROXY_HOST=t.16yun.cn
export PROXY_PORT=31111
export PROXY_USERNAME=your-username
export PROXY_PASSWORD=your-password
export TARGET_URL=https://httpbin.org/ip
基础用法
const puppeteer = require('puppeteer');
const host = process.env.PROXY_HOST || 't.16yun.cn';
const port = process.env.PROXY_PORT || '31111';
const user = process.env.PROXY_USERNAME || 'user';
const pass = process.env.PROXY_PASSWORD || 'password';
const target = process.env.TARGET_URL || 'https://httpbin.org/ip';
(async () => {
const browser = await puppeteer.launch({
headless: true,
args: [
`--proxy-server=http://${host}:${port}`,
'--no-sandbox',
'--disable-setuid-sandbox',
'--ignore-certificate-errors',
'--disable-web-security',
],
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
// 代理认证——Puppeteer 通过 page.authenticate 处理 407 挑战
await page.authenticate({ username: user, password: pass });
const resp = await page.goto(target, {
waitUntil: 'domcontentloaded',
timeout: 30000,
});
const text = await page.evaluate(() => document.body.innerText);
console.log(resp.status(), text);
await browser.close();
})().catch((e) => { console.error(e); process.exit(1); });
Playwright 与 Puppeteer 的代理配置差异
| 方面 | Playwright | Puppeteer |
|---|---|---|
| 代理配置 | launch({ proxy: { server, username, password } }) | --proxy-server 启动参数 |
| 认证方式 | 内置在 proxy 参数中 | page.authenticate() 单独调用 |
| 处理 407 | 自动响应 | page.authenticate() 注册回调 |
| Chrome 标志 | 无需额外参数 | 需要 --ignore-certificate-errors 等 |
页面导航与内容提取
// 等待页面加载完成
await page.goto(target, { waitUntil: 'networkidle0', timeout: 30000 });
// 获取渲染后的内容
const title = await page.title();
const text = await page.evaluate(() => document.body.innerText);
// 截图
await page.screenshot({ path: 'page.png', fullPage: true });
// 获取 HTML
const html = await page.content();
请求拦截
Puppeteer 支持在请求级别拦截和修改,可以配合 Proxy-Tunnel:
await page.setRequestInterception(true);
page.on('request', (request) => {
const headers = request.headers();
// 添加 Proxy-Tunnel(仅 HTTP 请求有效)
headers['Proxy-Tunnel'] = process.env.PROXY_TUNNEL || 'puppeteer-demo';
request.continue({ headers });
});
await page.goto('http://httpbin.org/ip');
注意:
setRequestInterception修改的是 HTTP 请求头,对 HTTPS 的 CONNECT 阶段无效。HTTPS Proxy-Tunnel 在浏览器层面不支持。
四种场景对照
| 场景 | Puppeteer 实现 | 说明 |
|---|---|---|
| A:强制切换 | 关闭 browser 重新启动 | 开销大,适合低频切换 |
| B:保持 IP | 复用 browser/page | 默认行为 |
| C-HTTP:Proxy-Tunnel | request.continue({ headers }) | 通过请求拦截添加 |
| C-HTTPS:Proxy-Tunnel | ❌ 不支持 | 浏览器不支持 CONNECT 阶段头注入 |
错误排查
curl -x http://$PROXY_USERNAME:$PROXY_PASSWORD@$PROXY_HOST:$PROXY_PORT https://httpbin.org/ip
| 现象 | 原因 | 解决 |
|---|---|---|
| 407 代理认证失败 | page.authenticate 未调用或凭据错误 | 确认调用顺序和凭据 |
| ERR_PROXY_CONNECTION_FAILED | 代理服务器不可达 | 检查 host:port 和网络 |
| 页面白屏 | 需要 JS 渲染 | 增加 waitUntil 等待时间 |
需要企业代理方案?
我们可根据目标站点、并发规模与稳定性目标提供定制方案。