Updated
How to Convert HTML and Web Pages to Markdown (for LLMs too)
1. Whole page with pandoc
curl -s -o page.html https://www.python.org/about/
pandoc page.html -f html -t gfm-raw_html -o page.mdgfm-raw_html gives GitHub-flavoured Markdown and drops the HTML that has no Markdown equivalent. The result is readable but starts with the site menu, the skip links and the footer.
2. Main content only (Python)
pip install beautifulsoup4 markdownify
from bs4 import BeautifulSoup
from markdownify import markdownify
soup = BeautifulSoup(open('page.html'), 'html.parser')
main = soup.find('main') or soup.find('article') or soup.body
for tag in main(['script', 'style', 'nav', 'footer', 'aside', 'form']):
tag.decompose()
print(markdownify(str(main), heading_style='ATX'))Measured on python.org/about: 41.3 KB of HTML, 11.4 KB of Markdown for the whole page, 3.7 KB for the main content with pandoc, 4.7 KB with markdownify. For an LLM, that is about nine to eleven times less text than the raw HTML.
3. Checks before feeding a model
- Relative links: make them absolute (base URL + path) or the model cannot cite them.
- Tables: simple tables become pipe tables; tables with merged cells are better kept as JSON rows.
- Chunks: split along headings and store the source URL and fetch date with each chunk.
4. Many URLs, or HTML mixed with PDF and DOCX
Plainprobe Document to Markdown converts HTML pages, PDF, DOCX, PPTX and scanned images from URLs into Markdown and JSON, with tables as JSON and optional RAG chunks, at $0.004 per document; failed documents are free. See also PDF to Markdown for LLMs and DOCX to Markdown.
Limits to keep in mind
- Pages rendered only by JavaScript need a headless browser first.
- Convert only pages you may process, and respect each site's terms and robots.txt.
FAQ
How do I convert a web page to Markdown?
Download the HTML, keep only the main content (the main or article element), then convert it with pandoc -f html -t gfm-raw_html or the Python library markdownify. Converting the whole page also converts menus, footers and cookie banners.
Which is better for HTML to Markdown, pandoc or markdownify?
Both give clean GitHub-flavoured Markdown. pandoc is a command-line tool that also handles DOCX and other formats; markdownify is a small Python library that is easy to call inside a scraper. The bigger difference comes from removing the page boilerplate first.
Why convert HTML to Markdown for an LLM?
Markdown keeps headings, lists, links and tables but drops tags and attributes, so the same content takes far fewer tokens. On python.org/about, 41 KB of HTML became 3.7 to 4.7 KB of Markdown once only the main content was kept.
Does it work on pages built with JavaScript?
Only if the HTML you download already contains the text. For single-page apps, render the page in a headless browser first and convert the rendered HTML.