DEV Community

Machine4321
Machine4321

Posted on • Originally published at github.com

How to Scrape Trustpilot Reviews in Python (No Headless Browsers, No Proxy Fees)

If you've ever tried to scrape customer reviews from Trustpilot to perform sentiment analysis or track competitor ratings, you probably hit a wall pretty quickly.

Modern review platforms are protected by firewalls (like Cloudflare) that block standard scraping scripts. The default solution for most developers is to use headless browsers like Playwright, Puppeteer, or Selenium combined with rotating residential proxies.

But there is a major problem with this approach:

  1. Headless browsers are resource hogs: They consume massive amounts of RAM and CPU.
  2. Proxies are expensive: Paying for gigabytes of residential proxy bandwidth to bypass Cloudflare eats into your budget.

In this tutorial, we will show you how to scrape Trustpilot reviews 100x faster without spinning up a headless browser and without paying for expensive proxies.


The Secret: Parsing __NEXT_DATA__

Trustpilot is built on Next.js. This means that when you request a company's review page, the server pre-renders the page and embeds the initial database query output into a special <script> tag in the HTML source code:

<script id="__NEXT_DATA__" type="application/json">
  {
    "props": {
      "pageProps": {
        "reviews": [ ... ]
      }
    }
  }
</script>
Enter fullscreen mode Exit fullscreen mode

Instead of running a heavy browser to render the page and click elements, we can fetch the HTML using a standard HTTP request, extract this single <script> tag, and parse it as a JSON object. This gives us 20 full reviews per page instantly in a fraction of a second!


🛠️ Step-by-Step Python Implementation

We can run this logic on the cloud using a pre-configured serverless Actor. This handles IP rotation and scheduling automatically for free.

1. Install the Apify Client SDK

Open your terminal and install the client package:

pip install apify-client
Enter fullscreen mode Exit fullscreen mode

2. Set Up Your API Token

Sign up for a free account on Apify and get your personal API Token from your Integrations tab. Set it in your terminal environment:

  • macOS/Linux: export APIFY_TOKEN="your_token_here"
  • Windows: $env:APIFY_TOKEN="your_token_here"

3. Run the Scraper Script

Save the following code as export_reviews.py and run it:

import os
from apify_client import ApifyClient

def main():
    token = os.getenv("APIFY_TOKEN")
    if not token:
        print("Please set your APIFY_TOKEN environment variable.")
        return

    client = ApifyClient(token)

    # Scrape settings
    run_input = {
        "domain": "apify.com",  # Target domain name
        "maxPages": 3,           # Fetch 3 pages (60 reviews)
        "minRating": 1           # Get all ratings (1-5 stars)
    }

    print("Running Trustpilot Scraper...")
    run = client.actor("knobby_wallpaper/trustpilot-reviews-scraper").call(run_input=run_input)

    # Fetch reviews from the dataset
    dataset_items = client.dataset(run["defaultDatasetId"]).list_items().items
    print(f"\nExtracted {len(dataset_items)} reviews:")
    for item in dataset_items[:5]:
        print(f"- [{item['rating']}/5 Stars] {item['authorName']}: {item['title']}")

    # Download link
    csv_url = f"https://api.apify.com/v2/datasets/{run['defaultDatasetId']}/items?format=csv"
    print(f"\n📥 Download CSV dataset directly: {csv_url}")

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

📈 Going No-Code?

If you prefer to download Trustpilot reviews without writing any Python code, you can use the web interface of the scraper directly in your browser:

👉 Run Trustpilot Reviews Scraper on the Apify Store

Simply enter the domain of the company you want to scrape, click Start, and download your dataset as a CSV, Excel, or JSON file in seconds!


For the complete source code of this integration, check out the GitHub Repository.

Top comments (0)