【问题标题】:Puppeteer - Connecting to HTTPS website through a proxy with authentication doesn't workPuppeteer - 通过具有身份验证的代理连接到 HTTPS 网站不起作用
【发布时间】:2020-10-31 20:51:31
【问题描述】:

过去 2 天我一直在尝试解决此问题,但未能成功。我到处都查过了,仍然没有解决方案。这是代码:

const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');

puppeteer.use(StealthPlugin());

const PROXY_SERVER_IP = 'IP.IP.IP.IP';
const PROXY_SERVER_PORT = '1234';
const PROXY_USERNAME = 'username';
const PROXY_PASSWORD = 'password';

(async () => {
  const browser = await puppeteer.launch({
    args: [`--proxy-server=http://${PROXY_SERVER_IP}:${PROXY_SERVER_PORT}`],
  });
  const page = await browser.newPage();

  await page.authenticate({
    username: PROXY_USERNAME,
    password: PROXY_PASSWORD,
  });

  await page.goto('https://www.google.ca/', {
    timeout: 0,
  });

  await page.screenshot({ path: 'test4.png', fullPage: true });

  await browser.close();
})();

我在 page.goto() 调用上收到导航超时错误,因为它只是由于某种原因挂起。我不知道为什么。当我放置不需要身份验证的代理时,它可以工作。由于这个问题,我正在考虑切换到另一种无头解决方案,我非常感谢一些帮助。

【问题讨论】:

  • 我知道剧作家至少有 chromium 和 firefox 的代理身份验证。
  • 我认为问题在于设置http身份验证而不是代理身份验证。尝试使用地址:http://user::password@ip:port,例如:http://leo:some%40password@192.168.15.80:3128(密码必须用encodeURIComponent编码),或者使用Proxy-Authentication http header。看这里:github.com/puppeteer/puppeteer/issues/676
  • @pguardiario 感谢您的建议,我实际上是在看剧作家,可能会转行。
  • @Leo 我已经尝试过该标头解决方案,但没有奏效:/。当您说“问题在于设置 http 身份验证而不是代理身份验证”时,您能否扩展您的意思?
  • @pguardiario 好的,所以我刚刚用 Playwright 尝试了这个,我得到了完全相同的问题!我知道代理不是问题,因为我可以通过它向google.com 发出 cURL 请求,一切正常..

标签: authentication https proxy puppeteer headless-browser


【解决方案1】:

所以我想通了。事实证明,由于某种原因,代理真的很糟糕。 Axios 和 cURL 给出快速响应的原因是因为它们只获取初始 HTML 代码,并且与无头浏览器不同,它们实际上并不对 HTML 文本做任何事情。使用无头浏览器,它们实际上也会对资产(css、图像等)和任何其他网络请求发出所有请求,并且它们都通过代理进行,因此速度要慢得多。当我尝试不同的代理(需要身份验证的代理)时,速度要快得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多