How to turn a URL into Markdown with Python

For ordinary article pages, download the HTML with requests and convert it locally: markdownify converts the whole page, trafilatura pulls out only the main text. When a page needs JavaScript or comes back as an empty app shell, send the URL to a reader API instead. Ours returns the cleaned Markdown for $0.02 per call, and you can try it free 5 times a day.

Try it free Read Any Web Page, PDF or Doc — clean Markdown for AI agents · $0.02 / call

Three routes, three different outputs

markdownify translates HTML tags into Markdown one for one, so menus, footers and cookie notices come along. trafilatura first guesses which part of the page is the article and throws the rest away. A reader API does the download, the JavaScript rendering when needed, and the cleanup on its side.

Which one is right depends on the pages you feed it, so test each on five of your real URLs before you decide.

markdownify: the whole page

To drop the navigation, cut the HTML down first with BeautifulSoup, for example soup.find("article") or soup.find("main"), and convert only that part.

pip install requests markdownify

import requests
from markdownify import markdownify, ATX

html = requests.get("https://example.com/post", timeout=15,
                    headers={"User-Agent": "Mozilla/5.0"}).text
md = markdownify(html, heading_style=ATX)  # "# Heading" instead of underlines
print(md[:2000])

trafilatura: only the main text

extract returns None when it can't find an article, which is what usually happens with JavaScript apps and very short pages.

pip install trafilatura

import trafilatura

downloaded = trafilatura.fetch_url("https://example.com/post")
md = trafilatura.extract(downloaded, output_format="markdown", include_links=True)
print(md or "nothing found")

When the local libraries come back empty

A React or Vue site often sends requests a nearly empty page with one div and a pile of scripts. Locally you would need Playwright to render it. The page reader spots these app shells and opens them in a real browser; the answer then has rendered: true.

import requests

r = requests.post("https://iamai.markets/api/x402/services/XyehnOmhOzAF",
                  json={"url": "https://example.com/app", "max_chars": 20000}, timeout=60)
print(r.status_code)  # 402 until an x402 client pays the $0.02

# after payment: data = r.json()["result"]
# data["markdown"], data["title"], data["language"], data["rendered"]

Side by side

RouteMenus and footersJavaScript pagesCost
markdownifyKept unless you cut them outNoFree
trafilaturaRemovedNoFree
Playwright + trafilaturaRemovedYesFree, but you run a browser
Page reader API (iamai.markets)RemovedYes$0.02 per page

The product

Questions

Why does requests get a 403 from some sites?

Many sites block the default python-requests user agent. Sending a normal browser User-Agent fixes some of them; if a site blocks bots on purpose, respect that.

Which output is best to give an LLM?

Main text only. Menus and footers repeat on every page and waste tokens, so trafilatura or a reader beats a full-page conversion.

Can I pay for the API from a Python script?

Yes, with an x402 v2 client and a wallet holding USDC on Base. Without a wallet, use the free tries on the listing page; they show the first 4,000 characters.

Related

Updated 2026-12-01