A Python library for easily interacting with Skrape.ai API with type-safe schemas using Pydantic
Project description
skrape-py
A Python library for easily interacting with Skrape.ai API. Define your scraping schema using Pydantic and get type-safe results.
Features
- 🛡️ Type-safe: Define your schemas using Pydantic and get fully typed results
- 🚀 Simple API: Just define a schema and get your data
- 🔄 Async Support: Built with async/await for efficient scraping
- 🧩 Minimal Dependencies: Built on top of proven libraries like Pydantic and httpx
Installation
pip install skrape-py
Or with Poetry:
poetry add skrape-py
Environment Setup
Setup your API key in .env:
SKRAPE_API_KEY="your_api_key_here"
Get your API key on Skrape.ai
Quick Start
from skrape import Skrape
from pydantic import BaseModel
from typing import List
import os
import asyncio
# Define your schema using Pydantic
class ProductSchema(BaseModel):
title: str
price: float
description: str
rating: float
async def main():
# Use the client as an async context manager
async with Skrape(api_key=os.getenv("SKRAPE_API_KEY")) as skrape:
# Extract data
response = await skrape.extract(
"https://example.com/product",
ProductSchema,
{"render_js": True} # Enable JavaScript rendering if needed
)
# Access the extracted data
product = response.result
print(f"Product: {product.title}")
print(f"Price: ${product.price}")
# Access rate limit information
usage = response.usage
print(f"Remaining credits: {usage.remaining}")
print(f"Rate limit reset: {usage.rateLimit.reset}")
# Run the async function
asyncio.run(main())
Schema Definition
We leverage Pydantic for defining schemas. Here's a more complex example:
from typing import List, Optional
from pydantic import BaseModel, Field
class Review(BaseModel):
author: str
rating: float = Field(ge=0, le=5) # Rating between 0 and 5
text: str
date: str
helpful_votes: Optional[int] = None
class ProductDetails(BaseModel):
title: str
price: float
description: str
rating: float
reviews: List[Review]
in_stock: bool
shipping_info: Optional[str] = None
For a comprehensive understanding of all available options and advanced schema configurations, we recommend exploring Pydantic's documentation.
API Options
When calling extract(), you can pass additional options:
response = await skrape.extract(
url,
schema,
{
"render_js": True, # Enable JavaScript rendering
# Add other options as needed
}
)
Error Handling
The library provides typed exceptions for better error handling:
from skrape import Skrape, SkrapeValidationError, SkrapeAPIError
async with Skrape(api_key=os.getenv("SKRAPE_API_KEY")) as skrape:
try:
response = await skrape.extract(url, schema)
except SkrapeValidationError as e:
# Schema validation failed
print(f"Data doesn't match schema: {e}")
except SkrapeAPIError as e:
# API request failed (e.g., rate limit exceeded, invalid API key)
print(f"API error: {e}")
Rate Limiting
The API response includes rate limit information that you can use to manage your requests:
response = await skrape.extract(url, schema)
usage = response.usage
print(f"Remaining credits: {usage.remaining}")
print(f"Rate limit info:")
print(f" - Remaining: {usage.rateLimit.remaining}")
print(f" - Base limit: {usage.rateLimit.baseLimit}")
print(f" - Burst limit: {usage.rateLimit.burstLimit}")
print(f" - Reset at: {usage.rateLimit.reset}")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file skrape_py-1.0.1.tar.gz.
File metadata
- Download URL: skrape_py-1.0.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.9.20 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5329b85adbf907961b5f6f3b0fe0799311dcbccc28e99880c5ae199e21021d1b
|
|
| MD5 |
7a7c95dc8fe8ecfb88267f168750f02b
|
|
| BLAKE2b-256 |
2699cf17c9007f8eefdacb886feae6ee7b9371a20cb61dbc560cd6cb36885a4e
|
File details
Details for the file skrape_py-1.0.1-py3-none-any.whl.
File metadata
- Download URL: skrape_py-1.0.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.9.20 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a999d80d2b40d1e5cef722248f4d8e5744b91a34705e7357514f44c3c233b1ac
|
|
| MD5 |
aeb5f05eee2ce5a6e222fdd17a04d59b
|
|
| BLAKE2b-256 |
183d1743f35c34327b28f7795307fadf383f6127a66d60fb73c6e0417ff96310
|