How to prevent web scraping

Intermediate4 min readUpdated

GET /learn/scraping/how-to-prevent-web-scraping/ Accept text/html text/markdown

Short answer

You cannot stop every scraper, because anything a browser can display a bot can read. You can make bulk extraction slow, costly and visible. Combine rate limits per IP and session, bot detection that does not trust the user agent, a hardened API, login for bulk data and honeypot links, and verify search engine crawlers so they keep working.

Treat scraping prevention as a cost problem. Each layer below removes a class of scraper or makes the remaining ones slower and easier to see, while real visitors and search engines get through. Start with the pages that give a scraper the most data per request.

Which pages and endpoints do scrapers target?

Check your logs for the paths with the highest share of requests from clients that never load CSS, JavaScript or images. They are usually:

  • Search results and category listings with page parameters
  • Product, listing or profile pages reached by sequential IDs
  • JSON endpoints that return prices, stock or search results
  • Sitemaps, which hand a scraper a complete URL list

Record normal traffic on each of these paths before you change anything, so you can tell whether a new rule stopped scrapers or real users. Can web scraping be detected? has log queries to start from.

How do you rate limit scrapers?

Rate limits stop the simplest scrapers, the ones that send hundreds of requests from one address. In nginx, put a tighter limit on expensive paths such as search:

nginx
http {
    limit_req_zone $binary_remote_addr zone=catalog:10m rate=2r/s;

    upstream app {
        server 127.0.0.1:8080;
    }

    server {
        listen 80;
        server_name example.com;

        location /search {
            limit_req zone=catalog burst=20 nodelay;
            limit_req_status 429;
            proxy_pass http://app;
        }

        location / {
            proxy_pass http://app;
        }
    }
}

Two requests per second with a burst of 20 is a starting point. Raise it if your logs show real users hitting 429 responses. Per-IP limits miss scrapers that spread requests over thousands of residential addresses, so also limit per session cookie or per account where you can. Rate limiting for bots covers what limits miss.

Add bot detection that does not trust the user agent

Any scraper can send a Chrome user agent. Detection has to check whether the rest of the request agrees with it:

  • The TLS fingerprint (JA4, a hash of how the client opens an encrypted connection) should match the browser the user agent claims
  • Headless and automated browsers leave traces in JavaScript properties and rendering, covered in headless browser detection
  • Real users scroll, move a pointer and pause unevenly, while scrapers move through pages at a steady rhythm
  • Many sessions from hosting providers, or one device fingerprint spread across hundreds of IP addresses, point to automation

Send suspicious sessions to an invisible challenge instead of blocking on one signal. A false positive then costs a real user a short delay.

Harden the API behind your pages

Scrapers prefer your JSON endpoints to your HTML, because the data comes back structured. On every public API:

  • Return only the fields the page displays, not the full database record
  • Cap page sizes on the server (for example a maximum of 50 items), whatever the client asks for
  • Use random or opaque IDs instead of sequential ones, so a scraper cannot count through your catalogue
  • Require a short-lived token issued when the page loads, and rate limit per token

OWASP describes this risk as unrestricted access to sensitive business flows. More API controls are in how to protect APIs from bots.

Require login for bulk data

If your value is in the full dataset, put bulk access behind an account. Keep the first pages of results public for search engines and new visitors, and require login for deep pagination, exports, contact details or full price history. Then apply quotas per account. A scraper must now create and age accounts, which turns the problem into fake account detection, and the terms accepted at signup give you a stronger legal position.

A honeypot link is invisible to people and disallowed in robots.txt, so only a bot that ignores both will request it:

html
<a href="/catalog/full-export" hidden tabindex="-1" aria-hidden="true">Full catalog</a>
txt
User-agent: *
Disallow: /catalog/full-export

Treat a hit as a strong signal for that session and IP address, and confirm it with other signals before blocking. More trap designs are in honeypots for bot detection.

How to stop competitors scraping your prices

Price scrapers usually hit product and category pages on a fixed schedule and read only the price element or the price API. Useful controls:

  • Serve prices from an API that requires the page-load token, and rate limit it per session
  • Challenge or slow suspected scrapers instead of blocking them outright, because a hard block tells the operator to change tactics
  • Do not show different prices to suspected bots, since every false positive would show a real shopper a wrong price
  • Give partners you want, such as comparison sites and marketplaces, a feed or an API key, so you can block everyone else on the same paths

Keep search engines and partners working

Before any rule goes live, exempt verified crawlers. Check Googlebot and Bingbot with reverse and forward DNS or their published IP lists, as explained in how to verify good bots, and never exempt a client because its user agent says Googlebot. After the change, compare crawl stats in Google Search Console and Bing Webmaster Tools with the previous week.

Tools that help

StopBots is published by Kairal, which is one of the tools listed. The list is not a ranking.

  • fail2ban is an open-source tool that bans IP addresses whose log lines match patterns you define, which is often enough against simple scrapers on a single server.
  • CrowdSec is an open-source engine that reads your logs, detects aggressive crawling and shares IP reputation across its community, and its free version suits small sites.
  • Cloudflare Bot Management gives each request a bot score you can act on in rules, a fit for sites already proxied through Cloudflare.
  • Kairal connects device, network and behavioural signals to detect scraping and lets you set protection per endpoint, with a preview mode that shows decisions before anything is blocked.
  • AWS WAF Bot Control is a managed rule group that labels and blocks common and targeted bots, suited to sites behind CloudFront or an AWS load balancer.

Common questions

Can websites block web scraping?

They can block most of it. Scrapers using plain HTTP libraries from cloud servers are cheap to stop, while scrapers running real browsers through residential proxies need layered detection, and a determined one may still collect some pages slowly.

Does Cloudflare prevent web scraping?

Cloudflare offers Bot Fight Mode on free plans, Super Bot Fight Mode on Pro, Business and Enterprise plans, and Bot Management with a bot score per request as an Enterprise add-on. Scrapers that behave like real browsers can get past the simpler modes, so test the setting against your own logs.

Will anti-scraping measures hurt my SEO?

Only if they block or slow verified search engine crawlers. Allowlist Googlebot and Bingbot after DNS verification, never by user agent alone, and watch the crawl stats report in Search Console for a week after each change.