Skip to main content

Official Python SDK for XCrawl - A powerful web scraping API service

Project description

XCrawl Python SDK

XCrawl Python SDK provides an interface to the XCrawl API, including scraping, search, sitemap discovery, and site crawling.

Table of Contents

Installation

Install from PyPI:

pip install xcrawl

Install from source:

git clone <repository-url>
cd <repo-dir>/xcrawl-sdk-py
pip install -e .

Quick Start

  1. Get an API key from xcrawl.com.
  2. Set XCRAWL_API_KEY or pass api_key directly.
export XCRAWL_API_KEY=your-api-key
from xcrawl import XcrawlClient

client = XcrawlClient()

# Sync scrape
sync_result = client.scrape('https://example.com', {
    'output': {'formats': ['markdown']}
})
print(sync_result['data']['markdown'])

# Async scrape + auto polling
job = client.scrape('https://example.com', {
    'mode': 'async',
    'output': {'formats': ['markdown', 'summary']}
})
result = client.wait_for_job(job['scrape_id'], timeout=60)
print(result['status'])

Core APIs

Scrape

result = client.scrape('https://example.com', {
    'proxy': {'location': 'US'},
    'request': {
        'device': 'desktop',
        'only_main_content': True,
        'block_ads': True,
    },
    'output': {
        'formats': ['html', 'markdown', 'links', 'screenshot'],
        'screenshot': 'full_page'
    }
})

Async Status Check

status = client.get_job_result('scrape-id-123')
if status['status'] == 'completed':
    print(status['data']['markdown'])
elif status['status'] == 'failed':
    print(status.get('message'))

Structured Extraction (json format)

output.json.prompt and output.json.json_schema are both optional.

result = client.scrape('https://example.com', {
    'output': {
        'formats': ['json'],
        'json': {
            'prompt': 'Extract product name and price',
            'json_schema': {
                'type': 'object',
                'properties': {
                    'name': {'type': 'string'},
                    'price': {'type': 'number'}
                }
            }
        }
    }
})

Search

result = client.search({
    'query': 'web scraping',
    'location': 'New York, NY',
    'language': 'en',
    'limit': 10
})
print(result['data'])

result['data'] is dynamic and may vary by backend response. Stable fields include credits_used and credits_detail.

Balance

balance = client.get_balance()
print(balance['credits'])
print(balance['expire_at'])
print(balance['expire_at_utc'])

Map

result = client.map('https://example.com', {
    'filter': r'.*\.html$',
    'limit': 1000,
    'include_subdomains': True,
    'ignore_query_parameters': True
})
print(result['data']['links'])

Crawl

job = client.crawl('https://example.com', {
    'crawler': {
        'limit': 100,
        'max_depth': 3,
        'include': [r'.*\.html$'],
        'exclude': [r'.*/admin/.*']
    },
    'output': {'formats': ['markdown']}
})

crawl_status = client.get_crawl_status(job['crawl_id'])
print(crawl_status['status'])

Webhook (Async)

client.scrape('https://example.com', {
    'mode': 'async',
    'webhook': {
        'url': 'https://your-server.com/webhook',
        'events': ['completed', 'failed']
    }
})

Configuration

client = XcrawlClient(
    api_key='your-api-key',
    api_url='https://run.xcrawl.com',
    timeout=60,
    max_retries=3,
    backoff_factor=0.5,
)

Retry behavior:

  • Retries: HTTP 5xx and network failures
  • No retry: HTTP 4xx and validation errors

Output Formats

Supported values in output.formats:

  • markdown
  • html
  • raw_html
  • links
  • summary
  • screenshot
  • json

If output.formats is omitted or set to [], the response returns metadata only.

Error Handling

from xcrawl import XcrawlClient, XcrawlError, JobTimeoutError

client = XcrawlClient(api_key='your-api-key')

try:
    result = client.scrape('https://example.com', {
        'output': {'formats': ['markdown']}
    })
    print(result['data']['markdown'])
except JobTimeoutError as error:
    print(f'Job {error.job_id} timed out after {error.timeout_seconds}s')
except XcrawlError as error:
    print(error.code, error.message, error.status, error.request_id)

Requirements

  • Python >= 3.8

Development

pip install -e ".[dev]"
pytest
black xcrawl
mypy xcrawl

License

MIT

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

xcrawl-1.1.2.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

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

xcrawl-1.1.2-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file xcrawl-1.1.2.tar.gz.

File metadata

  • Download URL: xcrawl-1.1.2.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for xcrawl-1.1.2.tar.gz
Algorithm Hash digest
SHA256 35eacefca4b75c8899fcef0f5b579e15c6e6bd638b77f9026b5faaa8236e9f1f
MD5 356588aa8fb251f070c378ce20f9c5bd
BLAKE2b-256 04b095e845026f2735d478f0d189220d5b174428ff408be8945533b81d940357

See more details on using hashes here.

File details

Details for the file xcrawl-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: xcrawl-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for xcrawl-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 42b702fe75e1da6885a5bf53a40c3c5a9d7486a81f1b23a140a5e13ef83196a7
MD5 cc7e541bc68fcd15624d31259bed13de
BLAKE2b-256 61f0a8e1a927d3d452c8aa25e280454b0c428e9a44c0617d2d12dde7827d6887

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