乐闻世界logo
搜索文章和话题

How to stop puppeteer follow redirects

1个答案

1

When using Puppeteer, to prevent automatic follow-redirects, you can intercept each request and use request.abort() to block requests that trigger redirects.

Here is a specific example:

javascript
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // Listen to page requests await page.setRequestInterception(true); // Enable request interception page.on('request', request => { // Check if the request is a navigation request // The 'document' type refers to main document navigation requests if (request.isNavigationRequest() && request.redirectChain().length) { // If it's a navigation request with a redirect chain, abort the request request.abort(); } else { // Continue with other requests normally request.continue(); } }); // Visit the page await page.goto('http://example.com'); // Other operations... await browser.close(); })();

This code enables request interception by setting page.setRequestInterception(true). When a request occurs, it triggers the listener defined in page.on('request', ...).

In this listener, we check if the request is a navigation request (i.e., page navigation) using request.isNavigationRequest(), and determine if the navigation request is part of a redirect chain using request.redirectChain().length. If it is a navigation request with a redirect chain, use request.abort() to block the request, preventing automatic follow-redirects.

Note that blocking redirects may cause incomplete page loading or functionality issues, as some redirects are part of the website's normal functionality. Therefore, use this technique cautiously based on specific scenarios.

2024年6月29日 12:07 回复

你的答案