Skip to main content

Official Python SDK for CrawlGate Search Engine API

Project description

CrawlGate Python SDK

Official Python SDK for CrawlGate - AI-powered web scraping platform.

PyPI version Python versions License: MIT

Installation

pip install crawlgate

Quick Start

from crawlgate import CrawlGateClient

# Initialize client
client = CrawlGateClient(api_key="sk_live_...")

# Scrape a single URL
doc = client.scrape("https://example.com")
print(doc.markdown)

Features

  • Scrape - Extract content from any URL
  • Crawl - Recursively crawl websites
  • Map - Discover all URLs on a website
  • Search - Web search with optional scraping
  • Extract - LLM-powered structured data extraction

Usage Examples

Scrape a URL

from crawlgate import CrawlGateClient

client = CrawlGateClient(api_key="sk_live_...")

# Basic scrape
doc = client.scrape("https://example.com")
print(doc.markdown)

# With options
doc = client.scrape(
    "https://example.com",
    engine="dynamic",  # Use headless browser
    formats=["markdown", "html"],
    only_main_content=True
)
print(doc.html)

Crawl a Website

# Crawl and wait for completion
job = client.crawl(
    "https://example.com",
    limit=50,
    engine="dynamic"
)

print(f"Crawled {job.completed} pages")
for page in job.data:
    print(f"- {page.url}: {len(page.markdown or '')} chars")

Async Crawl (Manual Polling)

import time

# Start crawl job
response = client.start_crawl("https://example.com", limit=100)
print(f"Job started: {response.id}")

# Poll for status
while True:
    job = client.get_crawl_status(response.id)
    print(f"Status: {job.status}, Completed: {job.completed}/{job.total}")

    if job.status in ["completed", "failed"]:
        break

    time.sleep(2)

# Process results
for page in job.data:
    print(page.url)

Map a Website

# Discover all URLs
result = client.map("https://example.com")

print(f"Found {result.count} URLs:")
for url in result.links:
    print(url)

Web Search

# Search the web
results = client.search(
    "best python web scraping libraries 2024",
    limit=10,
    lang="en",
    country="us"
)

for result in results.data:
    print(f"{result.title}: {result.url}")

# Search with scraping
results = client.search(
    "python tutorials",
    limit=5,
    scrape_options={"formats": ["markdown"]}
)

for result in results.data:
    print(f"\n{result.title}")
    print(result.markdown[:500] if result.markdown else "No content")

LLM Extraction

# Extract structured data
result = client.extract(
    urls=["https://example.com/product"],
    schema={
        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "price": {"type": "number"},
            "in_stock": {"type": "boolean"},
            "features": {
                "type": "array",
                "items": {"type": "string"}
            }
        },
        "required": ["name", "price"]
    },
    system_prompt="Extract product details from the page",
    provider="openai"
)

print(result.data)

Scrape with LLM Extraction

doc = client.scrape(
    "https://example.com/product",
    extract={
        "schema": {
            "type": "object",
            "properties": {
                "title": {"type": "string"},
                "price": {"type": "number"}
            }
        },
        "systemPrompt": "Extract the product info",
        "provider": "openai"
    }
)

print(doc.extract.data)

Batch Scrape

urls = [
    "https://example.com/page1",
    "https://example.com/page2",
    "https://example.com/page3"
]

# Scrape multiple URLs
job = client.batch_scrape(
    urls,
    options={"formats": ["markdown"], "engine": "smart"}
)

print(f"Scraped {job.completed} URLs")
for doc in job.data:
    print(f"- {doc.url}: {len(doc.markdown or '')} chars")

Usage Monitoring

# Check concurrency
concurrency = client.get_concurrency()
print(f"Using {concurrency.concurrency}/{concurrency.max_concurrency} slots")

# Check credits
credits = client.get_credit_usage()
print(f"Remaining credits: {credits.remaining_credits}")

Engine Types

Engine Description Best For
static Axios + Cheerio (fast) Simple pages, APIs
dynamic Headless browser (Playwright) JS-heavy sites
smart Auto-selects based on content General use, LLM extraction

Error Handling

from crawlgate import CrawlGateClient
from crawlgate.errors import (
    CrawlGateError,
    AuthenticationError,
    RateLimitError,
    ValidationError,
    TimeoutError
)

client = CrawlGateClient(api_key="sk_live_...")

try:
    doc = client.scrape("https://example.com")
except AuthenticationError:
    print("Invalid API key")
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after}s")
except ValidationError as e:
    print(f"Invalid request: {e.message}")
except TimeoutError:
    print("Request timed out")
except CrawlGateError as e:
    print(f"Error: {e.message}")

Environment Variables

export CRAWLGATE_API_KEY="sk_live_..."
export CRAWLGATE_API_URL="https://api.crawlgate.io"  # Optional
# API key will be read from environment
client = CrawlGateClient()

API Reference

CrawlGateClient

CrawlGateClient(
    api_key: str = None,      # API key (or CRAWLGATE_API_KEY env)
    api_url: str = None,      # Base URL (or CRAWLGATE_API_URL env)
    timeout: int = 90,        # Request timeout in seconds
    max_retries: int = 3      # Max retry attempts
)

Methods

Method Description
scrape(url, **options) Scrape a single URL
crawl(url, **options) Crawl website and wait for completion
start_crawl(url, **options) Start async crawl job
get_crawl_status(job_id) Get crawl job status
cancel_crawl(job_id) Cancel crawl job
map(url, **options) Discover URLs on a website
search(query, **options) Web search
extract(urls, schema, **options) LLM extraction
batch_scrape(urls, **options) Batch scrape URLs
get_concurrency() Get concurrency usage
get_credit_usage() Get credit usage

License

MIT License - see LICENSE for details.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crawlgate-1.0.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crawlgate-1.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file crawlgate-1.0.0.tar.gz.

File metadata

  • Download URL: crawlgate-1.0.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for crawlgate-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4465dff09f4b8c253cb16c5f0a29058a132203bad49efc4e904a1797b36d996f
MD5 8a0e98880d3ced9091f2567c89d23c58
BLAKE2b-256 ff39acdd0017618dcc7df0d01e9272a585c70f03f3d51a087a5d681959cf4d0a

See more details on using hashes here.

File details

Details for the file crawlgate-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: crawlgate-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for crawlgate-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 546185eee702db2fcf4ad69163d8d3a11c1d8f6c4330d3e3871235aa66e03a9d
MD5 df9d64ed59cf0212ba214f7af2cfb5aa
BLAKE2b-256 0bc58b3f9cfb597999b8e119411c8a4875eb824c7c068de522f37916c9c31013

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page