Python
Web Scraping
HTTP 403
Error Handling
HTTP Requests

How do I avoid HTTP error 403 when web scraping with Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

An HTTP 403 response means the server understood your request but refused to serve it. In scraping work, that usually means one of three things: your request is missing something a normal browser would send, the resource requires authentication or an official API, or the site is intentionally blocking automated access.

Start with the legitimate reasons first

Before changing code, ask the simplest questions:

  • does the site allow automated access
  • does the target require login
  • is there an official API you should use instead

If the site is explicitly disallowing programmatic access, the right answer is usually not "bypass the 403". It is to use an approved interface or stop.

A basic browser-like request

Some servers return 403 because the request is too bare. A requests call with no realistic headers can look obviously non-browser-like.

python
1import requests
2
3headers = {
4    "User-Agent": "Mozilla/5.0",
5    "Accept-Language": "en-US,en;q=0.9",
6}
7
8response = requests.get("https://example.com", headers=headers, timeout=10)
9print(response.status_code)

This does not "defeat" anti-bot systems, but it does fix genuinely incomplete requests for sites that expect ordinary browser headers.

Sessions and cookies matter

Some sites require a session cookie or a navigation flow before the protected page can be fetched.

python
1import requests
2
3session = requests.Session()
4session.headers.update({
5    "User-Agent": "Mozilla/5.0",
6    "Accept-Language": "en-US,en;q=0.9",
7})
8
9landing = session.get("https://example.com", timeout=10)
10page = session.get("https://example.com/protected-page", timeout=10)
11
12print(page.status_code)

If the site uses cookies, CSRF tokens, or login state, individual stateless requests may fail even though a browser appears to work.

Authentication is different from scraping

If the page is behind login, the issue is usually not "how do I avoid 403" but "how do I authenticate correctly". That may mean:

  • using an API token
  • logging in with a real account and session flow
  • using an official SDK or export feature

Trying random headers when the resource genuinely requires authorization is just debugging the wrong layer.

Rate and behavior still matter

Even on sites that permit access, overly aggressive request behavior can trigger blocking. Use respectful pacing, caching, and retry behavior.

python
1import time
2import requests
3
4for url in urls:
5    response = session.get(url, timeout=10)
6    time.sleep(1)

This is not about evading defenses. It is about avoiding self-inflicted rate-limit problems and behaving like a responsible client.

Use the right parser and workflow

Some 403 cases happen because the page depends on JavaScript-generated state, tokenized requests, or a documented backend endpoint the browser calls after loading. In those cases, inspect the network workflow first and determine whether there is an intended API rather than treating HTML scraping as the only option.

The best scraper is often the one that stops scraping HTML and uses a documented data endpoint instead.

Common Pitfalls

  • Treating every 403 as something to bypass rather than a sign to check site policy or authentication.
  • Sending bare requests with no realistic headers or session handling.
  • Ignoring cookies, CSRF tokens, or required login flow.
  • Hammering the site with high request volume and triggering rate-based blocking.
  • Scraping HTML when the site already exposes an official API or export mechanism.

Summary

  • A 403 usually means the server refuses the request, not that your Python code is syntactically wrong.
  • First check permission, authentication, and whether an official API exists.
  • Browser-like headers and session handling can fix legitimate request-shape issues.
  • Respect rate limits and site terms instead of trying to brute-force access.
  • Often the right fix is to use the intended interface, not to fight the 403 directly.

Course illustration
Course illustration

All Rights Reserved.