Optimizing Traffic
Your automation scripts often download unnecessary resources such as images, stylesheets, fonts, and other media files. These files consume bandwidth and can slow down your scraping operations.
You can block these unnecessary resources from loading entirely. By intercepting network requests and selectively blocking media files, you can focus only on the data you actually need.
This JavaScript code snippet works with both Playwright and Puppeteer to block media resources before they're downloaded:
await page.route('**/*', (route) => {
const request = route.request();
const type = request.resourceType();
if (['image', 'stylesheet', 'media', 'font'].includes(type)) {
return route.abort();
}
return route.continue();
});
Last updated
Was this helpful?