MainContentExtractor
MainContentExtractor is a Python library designed to extract the core content from HTML documents. It aims to address limitations found in other extraction tools, such as the inability to output clean HTML directly. The library is useful for LLM-related tasks and for feeding data into frameworks like LangChain and LlamaIndex by providing output in HTML, Text, or Markdown formats. It is currently at version 0.0.4, with a relatively active development cadence.
Warnings
- gotcha The library internally uses `trafilatura` and converts its XML output to HTML. This conversion is described as irreversible and may not perfectly match the original HTML structure.
- gotcha There can be confusion between the PyPI package name (`MainContentExtractor` - capitalized) and the Python module name for import (`main_content_extractor` - lowercase with underscores). A `ModuleNotFoundError` will occur if the import statement uses the incorrect casing or formatting.
- breaking Given the library's early stage (version 0.0.4), API stability is not guaranteed. Minor version updates may introduce breaking changes or significant modifications to the API without extensive deprecation warnings.
Install
-
pip install MainContentExtractor
Imports
- MainContentExtractor
from main_content_extractor import MainContentExtractor
Quickstart
import requests
from main_content_extractor import MainContentExtractor
# Example HTML content (or fetch from a URL)
html_content = """
<html>
<head><title>Example Page</title></head>
<body>
<header>Navigation Bar</header>
<main>
<h1>Important Article Title</h1>
<p>This is the main content paragraph.</p>
<p>Another paragraph with <a href="#">a link</a> inside.</p>
</main>
<footer>Footer content</footer>
</body>
</html>
"""
# Or, fetch from a URL (requires 'requests')
# url = "https://www.example.com"
# response = requests.get(url)
# response.encoding = 'utf-8'
# html_content = response.text
# Extract main content as HTML
extracted_html = MainContentExtractor.extract(html_content)
print("--- Extracted HTML ---")
print(extracted_html)
# Extract main content as Markdown
extracted_markdown = MainContentExtractor.extract(html_content, output_format="markdown")
print("\n--- Extracted Markdown ---")
print(extracted_markdown)
# Extract main content as plain text
extracted_text = MainContentExtractor.extract(html_content, output_format="text")
print("\n--- Extracted Text ---")
print(extracted_text)