dom-content-extraction
A Rust library for extracting main content from web pages using text density analysis. This is an implementation of the Content Extraction via Text Density (CETD) algorithm described in the paper by Fei Sun, Dandan Song and Lejian Liao: Content Extraction via Text Density.
What Problem Does This Solve?
Web pages often contain a lot of peripheral content like navigation menus, advertisements, footers, and sidebars. This makes it challenging to extract just the main content programmatically. This library helps solve this problem by:
- Analyzing the text density patterns in HTML documents
- Identifying content-rich sections versus navigational/peripheral elements
- Extracting the main content while filtering out noise
- Handling various HTML layouts and structures
Key Features
- Build a density tree representing text distribution in the HTML document
- Calculate composite text density using multiple metrics
- Extract main content blocks based on density patterns
- Unicode Support
- Support for nested HTML structures
- Efficient processing of large documents
- Error handling for malformed HTML
- Markdown output (optional feature) - Extract content as structured markdown
Unicode Support
DOM Content Extraction includes Unicode support for handling multilingual content:
- Proper character counting using Unicode grapheme clusters
- Unicode normalization (NFC) for consistent text representation
- Support for various writing systems including Latin, Cyrillic, and CJK scripts
- Accurate text density calculations across different languages
This ensures accurate content extraction from web pages in any language, with proper handling of:
- Combining characters (like accents in European languages)
- Bidirectional text
- Complex script rendering
- Multi-code-point graphemes (like emojis)
Usage
MSRV is 1.85 due to 2024 edition. Living on the edge!
Basic usage example:
use Html;
use get_content;
Installation
Add it it with:
or add to you Cargo.toml
= "0.3"
Optional Features
To enable markdown output support:
= { = "0.3", = ["markdown"] }
Documentation
Read the docs!
dom-content-extraction documentation
Library Usage with Markdown
use ;
let html = "<html><body><article><h1>Title</h1><p>Content</p></article></body></html>";
let document = parse_document;
let mut dtree = from_document?;
dtree.calculate_density_sum?;
// Extract as markdown
let markdown = extract_content_as_markdown?;
println!;
# Ok::
Run examples
Check examples.
This one will extract content from generated "lorem ipsum" page
This one prints node with highest density:
Extract content as markdown from lorem ipsum (requires markdown feature):
There is scoring example i'm trying to implement scoring. You will need to download GoldenStandard and finalrun-input datasets from:
https://sigwac.org.uk/cleaneval/
and unpack archives into data/ directory.
As far as i see there is problem opening some files:
But overall extraction works pretty well:
Overall Performance:
Files processed: 370
Average Precision: 0.87
Average Recall: 0.82
Average F1 Score: 0.75
Binary Usage
The crate includes a command-line binary tool dce (DOM Content Extraction) for
extracting main content from HTML documents. It supports both local files and
remote URLs as input sources.
Installation
The binary is included by default. You can install it using cargo:
Command-Line Options
dce [OPTIONS]
Options:
-u, --url <URL> URL to fetch HTML content from
-f, --file <FILE> Local HTML file to process
-o, --output <FILE> Output file (stdout if not specified)
--format <FORMAT> Output format [default: text] [possible values: text, markdown]
-h, --help Print help
-V, --version Print version
Note: Either --url or --file must be specified, but not both.
Markdown Output
To extract content as markdown format, use the --format markdown option:
# Extract as markdown from URL
# Extract as markdown from file and save to output
Note: Markdown output requires the markdown feature to be enabled.
Features
- URL Fetching: Automatically downloads HTML content from specified URLs
- Timeout Control: 30-second timeout for URL fetching to prevent hangs
- Error Handling: Comprehensive error messages for common failure cases
- Flexible Output: Write to file or stdout
- Temporary File Management: Automatic cleanup of downloaded content
- Markdown Support: Extract content as structured markdown (requires
markdownfeature)
Examples
Extract content from a URL and print to stdout:
Process a local HTML file and save to output file:
Extract from URL and save directly to file:
Dependencies
The binary functionality requires the following additional dependencies:
clap: Command-line argument parsingreqwest: HTTP client for URL fetchingtempfile: Temporary file managementurl: URL parsing and validationanyhow: Error handlinghtmd: HTML to markdown conversion (for markdown feature)
These dependencies are only included when building with the default cli
feature. The markdown feature requires the htmd dependency.