我正在尝试将puppeteer与我自己的代理一起使用,但似乎无法正常使用。 我的代理如下所示:
import * as h from 'http';
import * as hp from 'http-proxy';
import * as url from 'url';
const proxy = hp.createProxyServer({});
proxy.on('proxyReq', function (proxyReq, req, res, options) {
res.setHeader("Derp", "1231");
});
proxy.listen(8899);
h.createServer(function (req, res) {
const reqUrl = url.parse(req.url);
const targetHost = reqUrl.protocol + "//" + reqUrl.host;
proxy.web(req, res, { target: targetHost });
}).listen(3111);
然后,当我启动浏览器时,我会提供以下属性
const baseOptions: LaunchOptions = {
args:
[
"--no-sandbox",
"--disable-setuid-sandbox",
`--proxy-server=localhost:3111`,
],
timeout: 0,
ignoreHTTPSErrors: true
};
在goto
命令上失败,出现以下错误
Error: net::ERR_EMPTY_RESPONSE at http://natashaskitchen.com/
at navigate (.../node_modules/puppeteer/lib/Page.js:598:37)
at process._tickCallback (internal/process/next_tick.js:68:7)
如果有人有任何建议,将不胜感激,谢谢您!
答案 0 :(得分:0)
您必须使用page.authenticate()
来验证您的代理
https://pptr.dev/#?product=Puppeteer&version=v1.18.1&show=api-pageauthenticatecredentials
这就是我将Puppeteer与经过身份验证的代理一起使用的方法,效果很好!
const launchConfig = {
headless: true,
ignoreHTTPSErrors: true,
args: [`--proxy-server=11.321.4.23:8080`],
};
const browser = await puppeteer.launch(launchConfig);
const page = await browser.newPage();
await page.authenticate({
username: 'brucewayne',
password: 'darkknight',
});