Skip to content
LangGraph

Web Scraping Agent with LangGraph

Build an intelligent web scraping agent that fetches pages, extracts structured data, and handles pagination — powered by LangGraph.

web scrapingdata extractionHTTPparsing

Working Code

LangGraph
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
@tool
def fetch_url(url: str) -> str:
"""Fetch a webpage and return its content as markdown."""
import httpx
from markdownify import markdownify
response = httpx.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=15)
return markdownify(response.text)[:5000]
@tool
def extract_data(text: str, instruction: str) -> str:
"""Extract structured data from text based on instruction."""
# Uses the LLM itself to parse — no regex needed
return f"Extracting from {len(text)} chars: {instruction}"
agent = create_react_agent(
ChatOpenAI(model="gpt-4o"),
tools=[fetch_url, extract_data],
prompt="You are a web scraping agent. Fetch pages, extract the requested data, and return it in structured format. Respect robots.txt.",
)
result = await agent.ainvoke({
"messages": [("user", "Scrape the pricing page at example.com/pricing and extract all plan names and prices")]
})
print(result["messages"][-1].content)

Step by Step

1

Install dependencies

Install LangGraph and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the LangGraph agent with your tools, set the system prompt, and execute a query.

Ready to build with LangGraph?

Generate a production-ready project with LangGraph pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.