Python Example
This example demonstrates how to use the WebClassifAI API with Python using the requests
library.
Installation
pip install requests
Basic Example
import requests
import json
def classify_urls(urls, api_key, taxonomy="iab-1.0"):
"""
Classify a list of URLs using the WebClassifAI API.
Args:
urls (list): List of URLs to classify
api_key (str): Your API key
taxonomy (str): Classification taxonomy to use (default: "iab-1.0")
Returns:
dict: API response containing classification results
"""
endpoint = "https://api.classifai.com/v1"
payload = {
"urls": urls,
"api_key": api_key,
"taxonomy": taxonomy
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(endpoint, json=payload, headers=headers)
response.raise_for_status() # Raise an exception for bad status codes
return response.json()
except requests.exceptions.RequestException as e:
print(f"Error making request: {e}")
return None
# Example usage
if __name__ == "__main__":
# Your API key
API_KEY = "your_api_key_here"
# List of URLs to classify
urls = [
"https://www.example.com/",
"https://www.tech-blog.com/"
]
# Get classifications
results = classify_urls(urls, API_KEY)
if results:
# Process results
for result in results["results"]:
url = result["url"]
print(f"\nURL: {url}")
print(f"Scraped successfully: {result['scraped_successfully']}")
for prediction in result["predictions"]:
print("\nPrediction:")
if "tier1" in prediction:
tier1 = prediction["tier1"]
print(f"Tier 1: {tier1['name']} ({tier1['code']}) - Confidence: {tier1['confidence']:.2f}")
if "tier2" in prediction:
tier2 = prediction["tier2"]
print(f"Tier 2: {tier2['name']} ({tier2['code']}) - Confidence: {tier2['confidence']:.2f}")
Error Handling
The example includes basic error handling for network issues and invalid responses. For production use, you might want to add:
- Rate limiting
- Retry logic
- More detailed error handling
- Logging
Batch Processing
For optimal performance, send multiple URLs in a single request:
# Process up to 500 URLs in a single request
urls = ["https://example.com"] * 500
results = classify_urls(urls, API_KEY)
Async Example
Important: We strongly recommend batching URLs in a single request (up to the 500 URL limit) for optimal performance. Only use async execution when your limits exceed 500 URLs or your architecture specifically requires it.
import aiohttp
import asyncio
async def classify_urls_async(urls, api_key, taxonomy="iab-1.0"):
endpoint = "https://api.classifai.com/v1"
payload = {
"urls": urls,
"api_key": api_key,
"taxonomy": taxonomy
}
headers = {
"Content-Type": "application/json"
}
async with aiohttp.ClientSession() as session:
async with session.post(endpoint, json=payload, headers=headers) as response:
return await response.json()
# Example usage
async def main():
API_KEY = "your_api_key_here"
# For best performance, process URLs in batches of up to 500
# Only split into multiple async requests if absolutely necessary
batch_size = 500
all_urls = ["https://www.example.com/"] * 1000
batches = [all_urls[i:i+batch_size] for i in range(0, len(all_urls), batch_size)]
tasks = [classify_urls_async(batch, API_KEY) for batch in batches]
results = await asyncio.gather(*tasks)
for batch_result in results:
print(f"Processed batch with {len(batch_result['results'])} URLs")
if __name__ == "__main__":
asyncio.run(main())
Additional Resources
Last updated on