如何在 puppeteer 中设置多个自定义 HTTP headers?

How do I set multiple custom HTTP headers in puppeteer?

我正尝试在 https://kith.com/account/login?return_url=%2Faccount 使用 puppeteer 登录 当我登录并用音频解决验证码时,它检测到我是一个机器人,所以我试图更改请求 headers 看看是否有帮助,但找不到任何关于如何更改它们的信息。

我找到了这个,但它只显示 1 header:

await page.setRequestInterception(true)

        page.on('request', (request) => {
            const headers = request.headers();
            headers['X-Just-Must-Be-Request-In-All-Requests'] = '1';
            request.continue({
                headers
            });
        });

header 是一个数组,你可以根据需要添加多个

 page.on('request', (request) => {
            const headers = request.headers();
            headers['X-Just-Must-Be-Request-In-All-Requests'] = '1';
            headers['foo'] = 'bar';
            headers['foo2'] = 'bar2';
            
            request.continue({
                headers
            });
        });

您也可以使用专用的 puppeteer 方法设置多个 HTTP headers:page.setExtraHTTPHeaders

例如:

await page.setExtraHTTPHeaders({
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
        'upgrade-insecure-requests': '1',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9,en;q=0.8'
    })
await page.goto('...')