Pretor/pretor/tool_plugin/web_crawler/web_crawler.py

21 lines
763 B
Python

import httpx
from typing import Dict, Any
class WebCrawlerTool:
def __init__(self, timeout: int = 10):
self.timeout = timeout
async def crawl(self, url: str) -> Dict[str, Any]:
try:
async with httpx.AsyncClient(timeout=self.timeout) as client:
response = await client.get(url)
response.raise_for_status()
# Basic text extraction can happen here (e.g., stripping HTML tags manually or with a library later)
return {
"url": url,
"status_code": response.status_code,
"content_preview": response.text[:500]
}
except Exception as e:
return {"url": url, "error": str(e)}