Skip to main content
Back to blog

Web Scraping With LangChain & Oxylabs API

Agne Matuseviciute avatarvytenis kaubre avatar

Agnė Matusevičiūtė

Last updated by Vytenis Kaubrė

2025-10-03

5 min read

AI Summary:

This article demonstrates integrating LangChain with Oxylabs' Web Scraper API – enabling efficient, AI-driven web data extraction and analysis, effectively handling common scraping challenges like IP interruptions and CAPTCHAs. The integration allows for retrieving structured web data with minimal development effort.

LangChain is a robust framework designed for building AI applications that integrate Large Language Models (LLMs) with external data sources, workflows, and APIs.
By combining LangChain’s seamless pipeline capabilities with a tool like the Web Scraper API, you can collect public web data, all while handling common scraping-related hurdles that can disrupt your processes.

In today’s article, we’ll demonstrate how to integrate the LangChain framework with Web Scraper API. By the end of this article, you’ll be able to retrieve structured web data with minimal development effort. Let’s get started!

Common web scraping challenges 

In conversation about web data extraction, it’s inevitable that we’ll get to discussing its challenges. In practice, developers often run into problems that can complicate or disrupt the process – let’s take a closer look: 

  • IP interruptions and rate limiting

Websites often detect and hinder repeated requests from the same IP address to prevent automated scraping. They may also impose rate limits, capping the number of requests you can make within a specific time frame. Without proper measures, these restrictions can disrupt data collection.

  • CAPTCHAs and other traffic filtering systems

Websites implement CAPTCHAs and other complex web mechanisms to distinguish between human users and automated scripts. Handling these defenses requires sophisticated tools or external CAPTCHA-solving services, adding complexity and cost.

  • Large-scale scraping

As scraping projects grow, handling large volumes of data efficiently becomes a challenge. This includes managing storage, ensuring fast processing, and maintaining reliable infrastructure to handle numerous concurrent requests.

LangChain vs. regular scraping methods

Recently, frameworks like LangChain have introduced a new paradigm, leveraging large language models (LLMs) to interpret and process data in a more dynamic and context-aware manner.

Let’s take a look at a comparison of LangChain and regular web scraping methods across a few dimensions:

1. Purpose and use case 

  • LangChain: Combines web scraping with LLMs for automated data analysis and insights generation. Ideal for workflows that need both data extraction and AI-driven processing.

  • Regular scraping: Web scraping tools like Scrapy, BeautifulSoup, and Puppeteer are focused solely on data collection. Post-processing requires separate tools and scripts.

2. Handling dynamic content 

  • LangChain: When paired with APIs like Oxylabs, it seamlessly handles JavaScript-rendered content and handles any interruptions.

  • Regular scraping: Requires additional tools like Selenium or Puppeteer to handle dynamic content, which can increase complexity.

3. Data post-processing

  • LangChain: Built-in LLM integration allows for immediate tasks like summarization, sentiment analysis, and pattern recognition.

  • Regular scraping: Data analysis and transformation require custom scripts or separate libraries, adding more steps to the workflow.

4. Error handling and reliability

  • LangChain: Automatically manages challenges like CAPTCHAs and failed requests via integrated API solutions.

  • Regular scraping: Requires manual error handling with retries, proxy management, or third-party captcha-solving tools.

5. Scalability and workflow automation

  • LangChain: Scales efficiently, automating the entire pipeline from scraping to actionable insights.

  • Regular scraping: Scalability is possible with frameworks like Scrapy but often requires additional configurations and custom setups.

6. Ease of use

  • LangChain: Simplifies complex workflows, making it easier to integrate advanced features like AI with minimal setup.

  • Regular scraping: Requires more technical knowledge and effort to build and maintain robust scrapers.

All in all...

Choose LangChain for projects that benefit from seamless integration of data scraping and AI-driven analysis. Opt for regular web scraping when you need full control over the scraping process or for simpler, standalone data extraction tasks.

How to use LangChain with Web Scraper API

While the possibilities are endless, let's focus on a simple one for today. We'll write a few scripts that use Web Scraper API to handle the complexities of modern web scraping, including JavaScript rendering, request handling, and automatic data parsing. Web Scraper API gives developers programmatic control over enterprise-grade scraping, while LangChain, LangGraph, and LLMs handle the interpretation of extracted data throughout the pipeline. There are three main ways to integrate Web Scraper API:

  • By using the langchain-oxylabs tool to scrape asearch engines, e-commerce sites, travel platforms, and any other website

  • By using the Oxylabs MCP server to scrape Google, Amazon, and any other URL

  • By directly calling the API to scrape any URL and passing the data to LangChain

Let’s see all methods in action.

Get free credits

Claim your free credits to test Web Scraper API to get structured, ready-to-use data in JSON, XHR, screenshot, or Markdown formats for your project needs.

Up to 2K results

No credit card is required

Set the environment

First off, install the necessary libraries for all three integration methods at once. For the sake of the tutorial, we’ll use GPT models provided by OpenAI, but you can use any sort of model supported by the LangChain framework.

Inside a new project’s folder, initialize a Python virtual environment and activate it:

python -m venv .venv
source .venv/bin/activate

Then, run the following pip command to install the libraries in the activated .venv:

pip install -U langchain-oxylabs "langchain[openai]" langgraph langchain-mcp-adapters requests python-dotenv

Next, save your Oxylabs credentials and the LLM key as environment variables. You can easily do so by creating a .env file in your project’s directory and storing the authentication details as shown below:

OXYLABS_USERNAME=your-username
OXYLABS_PASSWORD=your-password
OPENAI_API_KEY=your-openai-key

You may also save environment variables system-wide using your terminal and thus skip the dotenv library altogether.

Integrate via langchain-oxylabs module

The langchain-oxylabs package has two components that enable LLMs to scrape:

  1. Google search results in real time and handle any interruptions along the way

  2. Any website using the Oxylabs document loader, allowing you to specify complete API payloads and return parsed results or in Markdown format

Here’s a basic implementation to get and analyze Google results:

import os
from dotenv import load_dotenv
from langchain_oxylabs import OxylabsSearchAPIWrapper, OxylabsSearchRun
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage


load_dotenv()
search = OxylabsSearchRun(
    wrapper=OxylabsSearchAPIWrapper(
        oxylabs_username=os.getenv("OXYLABS_USERNAME"),
        oxylabs_password=os.getenv("OXYLABS_PASSWORD")
    )
)

response_results = search.invoke({
    "query": "best restaurants in Los Angeles",
    "geo_location": "Los Angeles,United States"
})

llm = ChatOpenAI(model="gpt-5-nano", temperature=0)

analysis = llm.invoke([
    HumanMessage(content=f"""
    Based on the scraped search results:
    {response_results}
    Summarize the data with key insights.
    """)
])

print(analysis.content)

After getting the results, pass the scraped data to your AI workflow for processing. You may also let the AI use the scraper dynamically without specifying API parameters in the code. 

When executed, Web Scraper API will scrape Google for a given search term, the LLM will analyze the scraped data, and then answer the question.

Integrate via Oxylabs MCP server

The Oxylabs MCP server enables AI applications to use Oxylabs scraping infrastructure to scrape Google search results, Amazon search and product results, and any other website. Make sure you to install the uv package in your environment by following this installation guide. For example, on macOS, you can use Homebrew:

brew install uv

To keep things simple, let’s use LangGraph to create a basic AI agent that uses the Oxylabs MCP server as a tool. For a deeper understanding of how MCP compares to other AI protocols, see this MCP vs A2A comparison.

import os
import asyncio
from dotenv import load_dotenv
from langchain_mcp_adapters.sessions import create_session
from langchain_mcp_adapters.tools import load_mcp_tools
from langgraph.prebuilt import create_react_agent


load_dotenv()

config = {
    "transport": "stdio",
    "command": "uvx",
    "args": ["oxylabs-mcp"],
    "env": {
        "OXYLABS_USERNAME": os.getenv("OXYLABS_USERNAME"),
        "OXYLABS_PASSWORD": os.getenv("OXYLABS_PASSWORD"),
    }
}


async def main():
    async with create_session(config) as session:
        await session.initialize()
        tools = await load_mcp_tools(session)

        agent = create_react_agent("openai:gpt-5-nano", tools)

        result = await agent.ainvoke(
            {
                "messages": [
                    {
                        "role": "user",
                        "content": "Summarize this page: https://oxylabs.io/products/residential-proxy-pool"
                    }
                ]
            }
        )
        print(f"\n{result['messages'][-1].content}\n")


if __name__ == "__main__":
    asyncio.run(main())

When you run the code, the agent chooses a best scraping tool and scrapes public data to answer the question:

Integrate via direct API calls

If you want to have more control of the scraper, you can manually set up requests to the API. When you have some data scraped, you can then get all the data processed by an LLM using LangChain.

For this, you can write a simple function that accepts string content as a parameter, then formats that content into a provided prompt template and sends it for interpretation:

import os
import requests
from dotenv import load_dotenv
from langchain_openai import OpenAI
from langchain_core.prompts import PromptTemplate


load_dotenv()

# Define the prompt template
prompt = PromptTemplate.from_template(
    "Analyze the following website content and summarize key points: {content}"
)

# Compose the chain using the prompt and the LLM
chain = prompt | OpenAI()


def scrape_website(url):
    """Scrape the website using Oxylabs Web Scraper API"""
    payload = {
        "source": "universal",
        "url": url,
        "parse": True
    }

    response = requests.post(
        "https://realtime.oxylabs.io/v1/queries",
        auth=(os.getenv("OXYLABS_USERNAME"), os.getenv("OXYLABS_PASSWORD")),
        json=payload
    )

    if response.status_code == 200:
        data = response.json()
        content = data["results"][0]["content"]
        print(content)
        return str(content)
    else:
        print(f"Failed to scrape website: {response.text}")
        return None


def process_content(content):
    """Process the scraped content using LangChain"""
    if not content:
        print("No content to process.")
        return None

    result = chain.invoke({"content": content})
    return result


def main(url):
    print("Scraping website...")
    scraped_content = scrape_website(url)

    if scraped_content:
        print("Processing scraped content with LangChain...")
        analysis = process_content(scraped_content)

        print("\nProcessed Analysis:\n", analysis)
    else:
        print("No content scraped.")


# Example URL to scrape
url = "https://sandbox.oxylabs.io/products/1"
main(url)

Note: The script utilizes automatic result parsing provided by the Oxylabs Web Scraper API to make our lives easier. If you want to read more about the capabilities of the API, head over to the docs here.

When you run the code, you can see the AI-generated summary of the scraped page:

Conclusion

Oxylabs' Web Scraper API with LangChain makes an excellent solution for efficient web scraping and AI-driven analysis. The API handles common challenges, such as CAPTCHAs, while LangChain enables real-time data processing with LLMs – making it an ideal choice for large-scale, hassle-free data acquisition.

If you want to read more on the topic of utilizing AI for web scraping, check out these posts:

To better understand how the data you collect can be used to train and fine-tune AI models, take a look at our in-depth guide for AI training.

About the author

Agne Matuseviciute avatar

Agnė Matusevičiūtė

Technical Copywriter

With a background in philology and pedagogy, Agnė focuses on making complicated tech simple.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Oxylabs Web API Quick Start Guide
Oxylabs Web API Quick Start Guide
Dovydas Vėsa avatar

Dovydas Vėsa

2026-10-07

BeautifulSoup Tutorial - How to Parse Web Data With Python
BeautifulSoup Tutorial - How to Parse Web Data With Python
Authors avatar

Adomas Sulcas

2026-10-05

How to Scrape Walmart in 2026: A Step-by-Step Tutorial
How to Scrape Walmart in 2026: A Step-by-Step Tutorial
Enrika avatar

Enrika Pavlovskytė

2026-10-05

Explore Fast Search API

Organic search results scraper for AI/ML teams building apps, training models, and powering research.

Learn more
scrapingdigest

Get the latest news from data gathering world

Scale up your business with Oxylabs®

Explore Fast Search API

Organic search results scraper for AI/ML teams building apps, training models, and powering research.

Learn more