How can I deal with Trustpilot's page structure changes in my scraper?

Dealing with Trustpilot's page structure changes in your web scraper can be challenging, as it can cause your scraping code to break or return inaccurate data. Here are some strategies you can employ to make your scraper more resilient to such changes:

  1. Regular Monitoring and Updates:

    • Regularly check your scraper's performance and the structure of Trustpilot's pages.
    • Update your scraper's code as soon as you notice any changes that affect its functionality.
  2. Flexible Selectors:

    • Use more flexible and generic selectors that are less likely to change. For example, instead of relying on complex and specific CSS paths, use selectors based on class names or attributes that are less likely to be altered.
    • If possible, identify unique identifiers, like data attributes, which are less likely to change compared to classes or IDs used for styling.
  3. Robust Parsing Techniques:

    • Instead of relying on the exact structure, parse the entire section of interest and then search within that section for the data you need.
    • Use regular expressions or string searching techniques to find the patterns that indicate the data points you want to extract.
  4. Headless Browsers:

    • Employ headless browsers like Puppeteer (for JavaScript) or Selenium (for Python) to render JavaScript-heavy pages, which might be dynamically generating content.
  5. Error Handling:

    • Implement comprehensive error handling to catch exceptions related to changes in the page structure.
    • When an error is caught, log the issue, and if necessary, trigger an alert to notify you of the potential need for a code update.
  6. Machine Learning:

    • For advanced use cases, consider using machine learning models to identify data points on a page. This approach can be more resilient to changes but requires a lot of data to train the models and might be overkill for simpler scraping tasks.
  7. API Use (if available):

    • Check if Trustpilot offers an API for the data you need. Using an official API is always preferable to scraping as it's less prone to breaking due to structural changes on the website.
  8. Respect Legal and Ethical Considerations:

    • Be aware that scraping Trustpilot might violate their terms of service. Ensure compliance with legal and ethical guidelines when scraping websites.

Here's how you might adjust your code for flexibility in Python using BeautifulSoup:

from bs4 import BeautifulSoup
import requests

url = "https://www.trustpilot.com/review/example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# Use more flexible selectors to locate elements
# For example, if you're looking for review titles, which are less likely to change
for review in soup.find_all('article', {'class': 'review'}):
    title = review.find('h2', {'class': 'review-content__title'})
    if title:
        print(title.get_text(strip=True))

In JavaScript with Puppeteer:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.trustpilot.com/review/example.com');

  // Use more flexible selectors to locate elements
  const reviewTitles = await page.evaluate(() => {
    const titles = [];
    document.querySelectorAll('article.review').forEach((review) => {
      const titleElement = review.querySelector('h2.review-content__title');
      if (titleElement) {
        titles.push(titleElement.innerText.trim());
      }
    });
    return titles;
  });

  console.log(reviewTitles);

  await browser.close();
})();

In both examples, the selectors used are based on assumed consistent class names such as 'review' and 'review-content__title'. If Trustpilot changes these class names, you would need to update your selectors accordingly.

Remember that web scraping can be fragile due to the dynamic nature of web content. It's essential to build your scraper with an expectation of ongoing maintenance.

Related Questions

Get Started Now

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