Web pages frequently repeat content: navigation menus, footers, cookie notices, and syndicated articles all appear across many URLs. Without deduplication, these repeated segments pollute a corpus and waste storage. Trafilatura addresses this at two granularities — individual text segments within a page and whole documents across a crawl — using efficient in-memory data structures that add negligible overhead to the extraction pipeline.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/adbar/trafilatura/llms.txt
Use this file to discover all available pages before exploring further.
The deduplication functions were consolidated into
trafilatura.deduplication in version 1.10.0. They are no longer accessible from the filters or hashing submodules.Two types of deduplication
Segment-level (exact)
Uses a Least Recently Used (LRU) cache to detect and remove text segments that have appeared too many times. Best for boilerplate elements such as navigation, footers, and repeated callouts.
Document-level (near-duplicate)
Uses locality-sensitive hashing (SimHash / Charikar’s hash) to compute a fingerprint for each document. Comparing fingerprints identifies near-duplicate pages even when their text differs slightly.
Duplicate tracking is performed per thread. Each thread or process maintains its own independent cache and does not share state with other workers. This makes deduplication safe for parallel crawls without locking overhead.
Segment-level deduplication
Segment deduplication inspects each extracted text paragraph or block element. If the same text string has been seen more thanmax_repetitions times and its length exceeds min_duplcheck_size, it is flagged as a duplicate and excluded from the output.
Enabling in extraction functions
- extract() keyword argument
- bare_extraction() keyword argument
- Extractor options object
The duplicate_test() function
For lower-level control, callduplicate_test() directly on any lxml element. The function checks whether the element’s text content has been seen before, updates the LRU cache, and returns True if the segment should be considered a duplicate.
An lxml
_Element whose text content (including all descendant text nodes via itertext()) is checked against the cache.An
Extractor instance. The function reads two attributes:min_duplcheck_size— minimum character length for a segment to be evaluated (shorter segments are always kept).max_repetitions— maximum number of times a segment may appear before being flagged.
Tuning via settings.cfg
The two deduplication thresholds can be set globally insettings.cfg:
Extractor:
Document-level deduplication
Document-level deduplication works on whole texts rather than individual segments. The SimHash algorithm produces a compact fingerprint that reflects the overall content of a document. Two documents with very similar fingerprints are near-duplicates even if a few words or sentences differ.How SimHash works
Trafilatura implements Charikar’s SimHash (also called locality-sensitive hashing). The process:- Tokenizes the input text and removes punctuation.
- Computes a 64-bit BLAKE2b hash for each token.
- Accumulates a weighted bit-vector across all tokens.
- Converts the final vector to an integer hash.
0.0 (completely different) and 1.0 (identical).
The Simhash class
Text to hash. Pass an empty string when supplying
existing_hash.Number of bits in the hash. Higher values increase accuracy at the cost of storage.
A previously computed hash value (integer or hex string) to reuse. When provided,
inputstring is ignored.content_fingerprint()
content_fingerprint() is a convenience wrapper that creates a Simhash, calls .to_hex(), and returns the hash as a hexadecimal string — a compact, serializable representation suitable for storing in a database or file:
Deduplicating a corpus with fingerprints
generate_hash_filename()
When writing extracted content to disk,generate_hash_filename() produces a filename-safe string from the content hash. Files with identical or near-identical content receive the same or very similar names, making it easy to spot duplicates in the file system:
CLI: —deduplicate flag
Activate segment-level deduplication for all documents processed in a CLI session:Configuration reference
- settings.cfg
- Extractor options
- settings.py (package-wide)