Skip to content
LangChain

Web Scraping Agent with LangChain

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

web scrapingdata extractionHTTPparsing

Working Code

LangChain
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
@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}"
model = ChatOpenAI(model="gpt-4o")
model_with_tools = model.bind_tools([fetch_url, extract_data])
response = model_with_tools.invoke([
("system", "You are a web scraping agent. Fetch pages, extract the requested data, and return it in structured format. Respect robots.txt."),
("user", "Scrape the pricing page at example.com/pricing and extract all plan names and prices"),
])

Step by Step

1

Install dependencies

Install LangChain 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 LangChain agent with your tools, set the system prompt, and execute a query.

Ready to build with LangChain?

Generate a production-ready project with LangChain 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.