Skip to content
Pydantic AI

Web-Scraping-Agent mit Pydantic AI

Erstellen Sie einen intelligenten Web-Scraping-Agenten, der Seiten abruft, strukturierte Daten extrahiert und Paginierung verarbeitet — mit Pydantic AI.

web scrapingdata extractionHTTPparsing

Funktionierender Code

Pydantic AI
from pydantic_ai import Agent, RunContext
agent = Agent(
"openai:gpt-4o",
system_prompt="You are a web scraping agent. Fetch pages, extract the requested data, and return it in structured format. Respect robots.txt.",
)
@agent.tool
async def fetch_url(ctx: RunContext, url: str) -> str:
"""Fetch a webpage and return its content as markdown."""
import httpx
from markdownify import markdownify
async with httpx.AsyncClient() as client:
response = await client.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=15)
return markdownify(response.text)[:5000]
@agent.tool
async def extract_data(ctx: RunContext, text: str, instruction: str) -> str:
"""Extract structured data from text based on instruction."""
return f"Extracting from {len(text)} chars: {instruction}"
result = await agent.run("Scrape the pricing page at example.com/pricing and extract all plan names and prices")
print(result.output)

Schritt für Schritt

1

Abhängigkeiten installieren

Installieren Sie Pydantic AI und die benötigten Tools für diesen Anwendungsfall.

2

Tools definieren

Erstellen Sie domänenspezifische Tool-Funktionen, die Ihr Agent zur Interaktion mit externen Diensten verwenden wird.

3

Agent erstellen und ausführen

Initialisieren Sie den Pydantic AI-Agenten mit Ihren Tools, setzen Sie den System-Prompt und führen Sie eine Abfrage aus.

Bereit, mit Pydantic AI zu bauen?

Generieren Sie ein produktionsbereites Projekt mit vorkonfiguriertem Pydantic AI — FastAPI + Next.js, Auth, Streaming und mehr.

Loslegen

Bereit, deinen ersten Produktions-KI-Agenten zu bauen?

Open-Source-Tools, kampferprobte Patterns, null Boilerplate. Konfiguriere deinen Stack und shippe in Minuten — nicht Monaten.