Cloudscraper

1.2.71 · active · verified Thu Apr 09

Cloudscraper is a Python library built on top of the `requests` library, designed to bypass Cloudflare's anti-bot page (also known as "I'm Under Attack Mode" or IUAM). It achieves this by mimicking a real web browser, handling JavaScript challenges, and managing cookies automatically, allowing users to scrape websites protected by Cloudflare. The library is actively maintained, with frequent updates to adapt to Cloudflare's evolving security measures. The current PyPI version is 1.2.71, though a major version 3.0.0 has been released on GitHub with significant changes.

Warnings

Install

Imports

Quickstart

The simplest way to use `cloudscraper` is by calling `cloudscraper.create_scraper()`, which returns a `CloudScraper` instance. This instance behaves similarly to a `requests.Session` object. Any requests made through this session to Cloudflare-protected websites will automatically attempt to bypass the anti-bot measures. The script includes a placeholder URL and demonstrates basic error handling for a 403 status code, which is common if Cloudflare still blocks the request. It's recommended to set a realistic User-Agent for better stealth.

import cloudscraper
import os

# Instantiate a CloudScraper session. This object works like a requests.Session
scraper = cloudscraper.create_scraper(
    # Optionally, provide a `requests` Session object to base it on
    # sess=requests.Session(),
    # Or configure an interpreter, e.g., 'nodejs' if installed for better performance
    # interpreter='nodejs'
)

# Make a GET request to a Cloudflare-protected site
# Replace 'http://somesite.com' with your target URL
# For demonstration, we'll use a placeholder URL or a test site
# You might need to set headers, e.g., a User-Agent, for more realistic requests
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'}
url = os.environ.get('TARGET_URL', 'https://nowsecure.nl') # A common test site for bot detection

try:
    response = scraper.get(url, headers=headers)
    response.raise_for_status() # Raise an exception for bad status codes
    print(f"Successfully accessed {url} (Status: {response.status_code})")
    # print(response.text[:500]) # Print first 500 characters of content
except Exception as e:
    print(f"Failed to access {url}: {e}")
    if response.status_code == 403:
        print("Access denied (403 Forbidden). Cloudflare protection might be too strong or settings need adjustment.")

view raw JSON →