SDKs

LangChain SDK

Use MrScraper as LangChain tools via the langchain-mrscraper package to fetch web pages, search the web, and check account usage from agents.

The langchain-mrscraper package exposes the MrScraper API as LangChain BaseTool instances, so AI agents can fetch web pages as HTML, Markdown, or screenshots, search the web, and check account usage. It wraps the Python SDK (mrscraper-sdk), which is installed as a dependency. MrScraper is also listed in LangChain's web browsing tools.

Tools-First Integration

This integration provides tools that agents can explicitly call—not a document loader. For deterministic "URL → documents" ingestion into vector stores, a document loader is typically a better fit. MrScraper may offer that in a separate package later.

Requirements

  • Python 3.9 or higher
  • mrscraper-sdk 0.9.x and langchain-core 0.3 or later (installed as dependencies)

Package Information

This page documents version 0.5.0. See langchain-mrscraper on PyPI for the latest release, version history, and installation details.

Installation

Install the package from PyPI using pip:

pip install -U langchain-mrscraper

This installs both the LangChain integration and the underlying MrScraper SDK client.

Authentication

Set your API token from the MrScraper dashboard as an environment variable:

import os

os.environ["MRSCRAPER_API_KEY"] = "MRSCRAPER_API_TOKEN"

Authentication Options

The tools resolve the token in this order:

  1. The token parameter, such as MrScraperToolkit(token="...")
  2. The mrscraper_api_key parameter, such as load_mrscraper_tools(mrscraper_api_key="...")
  3. The MRSCRAPER_API_KEY environment variable
  4. The MRSCRAPER_API_TOKEN environment variable

You can also pass an existing mrscraper.MrScraper client with client=....

For more information on generating API tokens, see the Generate Token guide.

Quick Start

Using MrScraperToolkit

Load all three tools with the toolkit:

from langchain_mrscraper import MrScraperToolkit

# Using environment variable
tools = MrScraperToolkit().get_tools()

# Or with explicit token
tools = MrScraperToolkit(token="MRSCRAPER_API_TOKEN").get_tools()

Using Convenience Loader

Alternatively, use the convenience function. It accepts the same configuration as the toolkit:

from langchain_mrscraper import load_mrscraper_tools

tools = load_mrscraper_tools()

Using with AI Agents

from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
from langchain_mrscraper import MrScraperToolkit

# Load MrScraper tools
tools = MrScraperToolkit(token="MRSCRAPER_API_TOKEN").get_tools()

# Create agent with OpenAI
agent = create_react_agent(ChatOpenAI(model="gpt-4o-mini"), tools)

# Use the agent
response = agent.invoke({
    "messages": [{"role": "user", "content": "Search the web for MrScraper and summarize the top result."}]
})

Prerequisites

This example requires langgraph and langchain-openai packages:

pip install langgraph langchain-openai

Available Tools

ClassTool nameSDK methodPurpose
MrScraperScraperAPImrscraper_scraper_apiscraper_apiFetch a public web page as rendered HTML, Markdown, screenshot, or a combination
MrScraperWebSearchmrscraper_web_searchweb_searchSearch the web to retrieve real-time search results, including relevant website URLs, titles, and descriptions
MrScraperAccountmrscraper_accountaccountRead account identity, subscription, quota, token usage, and rate limits

Tool Outputs

Every tool returns a pretty-printed JSON string containing the SDK's status_code, data, and headers.

Core Methods

Scraper API

Fetch a public web page as rendered HTML, Markdown, screenshot, or a combination.

import base64
import json
from langchain_mrscraper import MrScraperScraperAPI

tool = MrScraperScraperAPI(token="MRSCRAPER_API_TOKEN")

# HTML only (the default) returns the HTML string in data
page = json.loads(tool.invoke({"url": "https://example.com"}))
print(page["data"][:200])

# Markdown or a screenshot returns an object with one entry per output
combined = json.loads(tool.invoke({
    "url": "https://example.com",
    "html": True,
    "markdown": True,
    "screenshot": True,
}))
print(combined["data"]["markdown"][:200])
image = base64.b64decode(combined["data"]["screenshot"])

At least one of html, markdown, or screenshot must be True. The screenshot is a base64 string. Setting screenshot=True enables browser rendering, so combining it with browser_rendering=False is rejected.

Parameters:

ParameterTypeRequiredDefaultDescription
urlstringYes-HTTP or HTTPS URL to fetch
htmlbooleanNoTrueReturn the rendered page HTML
markdownbooleanNoFalseReturn the page as Markdown
screenshotbooleanNoFalseReturn a base64 screenshot. Enables browser rendering
timeoutintegerNo120Page timeout in seconds (1-300)
geo_codestringNo"US"Geographic country code
proxy_countrystringNoNoneProxy country code
block_resourcesbooleanNoFalseBlock images, CSS, and fonts
browser_renderingbooleanNoNoneTrue forces JavaScript rendering, False disables it, None lets the service decide
superbooleanNoFalseUse real-device routing
use_proxybooleanNoTrueUse a proxy for this request
proxystringNoNoneCustom proxy in username:password@host:port format
wait_for_selectorstringNoNoneCSS selector to wait for
wait_untilstringNoNonePage lifecycle event to wait for, such as "domcontentloaded"
home_pagebooleanNoFalseVisit the site's root page first
bypass_proxybooleanNoFalseLoad static assets such as images, fonts and stylesheets outside the residential proxy to save bandwidth
cookie_jarstringNoNoneCookie configuration string
headersdict | stringNoNoneTarget request headers, as a dictionary or JSON string
max_retriesintegerNo3Maximum retries, 0 or greater
token_capintegerNoNoneMaximum retry-token budget, 0 or greater
streambooleanNoNoneRequest a streamed response
discard_token_secretstringNo""Discard-token secret, if required

For more examples of what you can fetch, see Scrape HTML, Scrape Markdown, and Get Screenshot.

Search the web to retrieve real-time search results, including relevant website URLs, titles, and descriptions.

import json
from langchain_mrscraper import MrScraperWebSearch

tool = MrScraperWebSearch(token="MRSCRAPER_API_TOKEN")

result = json.loads(tool.invoke({
    "query": "python web scraping",
    "country": "us",
    "num_results": 10,
}))
for item in result["data"]["data"]:
    print(item["rank"], item["title"], item["url"])

Parameters:

ParameterTypeRequiredDefaultDescription
querystringYes-Search query. Must not be blank
countrystringNoNoneCountry code for localized results, such as "us". Omit to use the service default
num_resultsintegerNo10Number of results to return (10-100)
timeoutfloatNo600.0HTTP timeout in seconds

The response data contains success, message, and data, a list of ranked results. Each result has rank, title, url, description, and source.

Account

Read account identity, subscription status, token usage and limits, remaining quota, and rate limits.

from langchain_mrscraper import MrScraperAccount

tool = MrScraperAccount(token="MRSCRAPER_API_TOKEN")
print(tool.invoke({}))

Parameters:

ParameterTypeRequiredDefaultDescription
timeoutfloatNo30.0HTTP timeout in seconds

See Account in the Python SDK documentation for the fields in the account summary.

Tool Loading Options

You can obtain tools in several ways:

1. Toolkit (Recommended)

from langchain_mrscraper import MrScraperToolkit

tools = MrScraperToolkit(token="MRSCRAPER_API_TOKEN").get_tools()

2. Convenience Loader

from langchain_mrscraper import load_mrscraper_tools

tools = load_mrscraper_tools(token="MRSCRAPER_API_TOKEN")

3. Selective Tool Loading

tool_names filters by the exact tool names. An empty list returns no tools.

from langchain_mrscraper import MrScraperToolkit

tools = MrScraperToolkit(
    token="MRSCRAPER_API_TOKEN",
    tool_names=["mrscraper_scraper_api", "mrscraper_web_search"],
).get_tools()

4. Per-Tool Constructors

from langchain_mrscraper import MrScraperScraperAPI

scraper_api = MrScraperScraperAPI(token="MRSCRAPER_API_TOKEN")

5. Shared SDK Client

from mrscraper import MrScraper
from langchain_mrscraper import MrScraperToolkit

client = MrScraper(token="MRSCRAPER_API_TOKEN")
tools = MrScraperToolkit(client=client).get_tools()

Async Usage

Every tool supports both .invoke(...) and await .ainvoke(...):

import asyncio
from langchain_mrscraper import MrScraperScraperAPI

async def async_scrape():
    tool = MrScraperScraperAPI(token="MRSCRAPER_API_TOKEN")
    output = await tool.ainvoke({
        "url": "https://example.com",
        "timeout": 120,
    })
    print(output)

asyncio.run(async_scrape())

Synchronous .invoke(...) calls also work inside notebooks that already have a running event loop.

Best Practices

Token Management

Store your API token securely:

import os

# Load from .env file
from dotenv import load_dotenv
load_dotenv()

token = os.getenv("MRSCRAPER_API_KEY")

Error Handling

Each tool validates its input before sending a request. Invalid URLs, out-of-range limits, and unsupported fields are rejected by the tool schema. API, authentication, and network errors are raised from the Python SDK.

from langchain_mrscraper import MrScraperScraperAPI

try:
    tool = MrScraperScraperAPI(token="MRSCRAPER_API_TOKEN")
    output = tool.invoke({"url": "https://example.com"})
except Exception as e:
    print(f"Scraping error: {e}")

Tool Selection

Only load the tools your agent needs:

from langchain_mrscraper import MrScraperToolkit

# Good: Only load needed tools
tools = MrScraperToolkit(tool_names=["mrscraper_web_search"]).get_tools()

# Loads all three tools
tools = MrScraperToolkit().get_tools()

Migrating from 0.3.x

Version 0.5.0 requires mrscraper-sdk>=0.9.0,<0.10.0, which exposes exactly three public methods. The toolkit now returns three tools instead of ten.

0.3.x tool0.5.0 replacement
MrScraperHTML (mrscraper_scraper_html)MrScraperScraperAPI with {"url": ...}
MrScraperMarkdown (mrscraper_scraper_markdown)MrScraperScraperAPI with {"url": ..., "html": False, "markdown": True}
MrScraperScreenshot (mrscraper_get_screenshot)MrScraperScraperAPI with {"url": ..., "html": False, "screenshot": True}
MrScraperJSON, MrScraperMultiplePage, MrScraperSitemap, MrScraperBrand, MrScraperGetBrandRemoved. Fetch pages with MrScraperScraperAPI and let your model extract data, or pin langchain-mrscraper<0.4
MrScraperWebSearch (url, raw, format, region, language, page, render_js)MrScraperWebSearch (query, country, num_results, timeout)
MrScraperAccountUnchanged
MrScraperFetchHTML import aliasRemoved. Use MrScraperScraperAPI
  • Screenshots now come back as a base64 string in data["screenshot"], together with any other requested outputs. The 0.3.x screenshot full_page option and its timeout=300 and wait_until="domcontentloaded" defaults no longer exist; pass timeout and wait_until explicitly when needed.
  • Update tool_names filters to mrscraper_scraper_api, mrscraper_web_search, and mrscraper_account.
  • The scraper and result tools from earlier versions (create, rerun, and bulk-rerun scrapers, and result listing) remain removed. Call the REST API directly for scrapers and results.

On this page