Logo New Black

Comprehensive Guide: How to Capture XHR Requests Puppeteer with Ease

In the intricate world of web development, capturing XMLHttpRequests (XHR) is a critical skill for those involved in web scraping and data analysis. Utilizing Puppeteer, a Node.js library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol, enables developers to automate this process with precision and efficiency. This guide focuses on the integration of Puppeteer with Python, a powerful combination that enhances the ability to monitor, capture, and analyze XHR requests and responses. By leveraging the page.on() method, developers can easily add callbacks for request and response events, thereby gaining access to a treasure trove of dynamic data loaded on web pages. For individuals and organizations aiming to maximize their web scraping efforts, exploring a web scraper API can significantly streamline the process, offering advanced tools and services designed to overcome the challenges of web data extraction.

const puppeteer = require('puppeteer');

function run() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  // capture background requests:
  await page.setRequestInterception(true);
  page.on('request', request => {
    if (request.resourceType() === 'xhr') {
      // we can block these requests with:
    } else {
  // capture background responses:
  page.on('response', response => {
    if (response.resourceType() === 'xhr') {
  await browser.close();


These background requests often contain crucial dynamic data. Blocking certain requests can also decrease the bandwidth consumed by the scraper. For more information on this, see how to block resources in Puppeteer.