How do I handle Trustpilot's infinite scroll when scraping reviews?

Handling infinite scroll on websites like Trustpilot can be challenging when scraping reviews because the content is usually loaded dynamically as the user scrolls down the page. This means that the initial HTML document you retrieve doesn't contain all the data. Instead, subsequent requests are made to the server to fetch more content. Here's how you can approach this problem:

Using Selenium or Puppeteer

One way to handle infinite scrolling is by using browser automation tools such as Selenium for Python or Puppeteer for JavaScript. These tools can simulate user actions like scrolling, which trigger the loading of more content.

Python with Selenium

Here's a basic example using Python and Selenium:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Initialize the browser
browser = webdriver.Chrome()

# Scroll down to the bottom of the page
while True:
    # Scroll down to bottom

    # Wait to load page

    # Calculate new scroll height and compare with last scroll height
    new_height = browser.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
    last_height = new_height

# Now you can parse the page for reviews
# ...

# Don't forget to close the browser

Be sure to replace {company_url} with the actual company's page you want to scrape. Also, note that this code lacks exception handling and might need adjustments for specific timing and loading conditions of the page.

JavaScript with Puppeteer

Here's an equivalent example using JavaScript and Puppeteer:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('{company_url}');

  let previousHeight;
  while (true) {
    const currentHeight = await page.evaluate('document.body.scrollHeight');
    await page.evaluate('window.scrollTo(0, document.body.scrollHeight)');
    await page.waitForFunction(`document.body.scrollHeight > ${previousHeight}`);
    await page.waitForTimeout(5000); // Wait for 5 seconds
    previousHeight = currentHeight;

  // Now you can parse the page for reviews
  // ...

  await browser.close();

Replace {company_url} with the actual page you are trying to scrape. This script has the same limitations as the Python example and may require fine-tuning.

Using API (if available)

Sometimes websites like Trustpilot have an API that they use to fetch new reviews when scrolling. If you can figure out the API endpoints and how they are called, you can send requests to these endpoints directly and retrieve the data in a more efficient and reliable way than web scraping.

Here's a general approach:

  1. Open the website in a browser and use the Developer Tools (usually by pressing F12) to inspect the network activity.
  2. Scroll down to trigger the infinite scroll and look for XHR (XMLHttpRequest) or Fetch requests that are being made.
  3. Analyze the request to understand the API endpoint, request parameters, headers, and the way pagination is handled.
  4. Write a script that mimics these requests to fetch the data.

This method is generally preferred when available because it is less resource-intensive and less likely to break with website updates. However, it may not be possible or allowed by the site's terms of service.

Ethical Considerations

When scraping websites, especially with automated tools like Selenium or Puppeteer, it is important to consider the ethical implications and the website's terms of service. Many websites prohibit scraping in their terms, and excessive automated access can put a strain on their servers. Always be respectful and avoid scraping at a rate that could be considered abusive. Additionally, consider the legal implications in your jurisdiction and the data privacy laws that may apply to the data you are scraping.

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping