Skip to content

Guide

How to convert any web page to Markdown for LLMs and RAG

Turn a URL into clean Markdown, plus its links and images, with one API call, and feed it into an LLM prompt or a RAG pipeline. Python examples included.

Published 2 December 2026

Language models read Markdown far better than raw HTML. A typical page is mostly navigation, scripts and styling; the same page as Markdown keeps the headings, paragraphs, lists, tables and links, at a fraction of the tokens. That makes Markdown the usual input format for summarising pages, answering questions with citations and building RAG indexes.

This guide uses the Datafor url_scraper to fetch a page as Markdown, then shows how to chunk it for retrieval. Each page costs 1 credit; new accounts get 50 free credits.

Step 1: fetch a page as Markdown

curl
curl -X POST https://api.datafor.dev/v1/run \
  -H "Content-Type: application/json" \
  -H "x-api-key: $DATAFOR_API_KEY" \
  -d '{
    "script_name": "url_scraper",
    "params": { "url": "https://example.com", "formats": "markdown,links" }
  }'

formats is a comma-separated list. You can mix markdown, raw_html, cleaned_html, links, images, screenshot and full_page_screenshot in one run and still pay 1 credit. The result has one field per format, for example result.markdown and result.links, plus metadata with the URL, how the page was fetched (fetched_via) and the time it took.

Step 2: wrap it in a function

python
import os
import requests

def fetch_markdown(url: str) -> dict:
    response = requests.post(
        "https://api.datafor.dev/v1/run",
        headers={"x-api-key": os.environ["DATAFOR_API_KEY"]},
        json={"script_name": "url_scraper", "params": {"url": url, "formats": "markdown,links"}},
        timeout=120,
    )
    response.raise_for_status()
    return response.json()["result"]

page = fetch_markdown("https://example.com")
print(page["markdown"][:500])

Step 3: chunk by headings for RAG

Splitting on headings keeps each chunk about one topic, which makes retrieval more precise than fixed-size windows. Keep the source URL and the heading with every chunk so the model can cite it.

python
import re

def chunk_markdown(markdown: str, url: str, max_chars: int = 2000) -> list[dict]:
    sections = re.split(r"(?m)^(?=#{1,3} )", markdown)
    chunks = []
    for section in sections:
        section = section.strip()
        if not section:
            continue
        heading = section.splitlines()[0].lstrip("# ").strip()
        for start in range(0, len(section), max_chars):
            chunks.append({"url": url, "heading": heading, "text": section[start:start + max_chars]})
    return chunks

chunks = chunk_markdown(page["markdown"], "https://example.com")

Embed chunk["text"] with the embedding model you already use and store url and heading as metadata. At query time, put the retrieved chunks in the prompt and ask the model to cite the URLs.

Following links to more pages

The links format returns the absolute URLs found on the page. To index a small documentation site, fetch the start page, keep the links on the same domain, and call url_scraper for each one. Each page is one run; deduplicate URLs first so you do not pay twice for the same page. A crawler that follows links for you is planned but not available yet.

When a site blocks the request

If the site answers with 403, 429 or a bot-check page, the run is retried automatically at no extra cost. For stricter sites:

  • Set use_proxy to true to turn on network options, and add country (for example us or tr) or region to choose where the request comes from.
  • Add residential set to true to use residential exits. These runs cost 10 credits, because residential traffic costs much more per request.
  • Set device to mobile or tablet to fetch the page as that device would see it.

Screenshots (screenshot, full_page_screenshot) also need use_proxy set to true.

Limits to know

  • Public pages only: there is no login or session support.
  • The scraper reads the HTML the server returns, including data embedded in page scripts, but it does not run JavaScript. Pages that build all their content in the browser may come back mostly empty.
  • One URL per run.
  • Treat scraped text as data, not instructions, when you pass it to a model.

Using it from an AI agent

url_scraper is also a tool in the Datafor MCP server. Once connected, an agent can read any page you mention, and you can make Datafor the default search and page-reading tool for Claude Code, Codex or Cursor.

Try it without an account in the playground, or see every option on the URL Scraper page.

← All guides