Sub topic 6
Finding exact duplicates with content hashes
This chapter assumes you can read a crawl export in pandas, know what HTTP status codes mean, and have already separated main content from boilerplate. Here you will find pages whose extracted text is identical.
Why hash the text
Comparing every page with every other page is slow. A content hash avoids this. A hash function turns any text into a short fixed-length string. Identical text always gives the same string, and different text almost never does. You compute one hash per page, then group rows that share a hash. That takes one pass over the crawl.
SHA-256 is a good default. You are not defending against an attacker, but its collisions are so unlikely that you can treat a shared hash as identical text.
The working model
The hash only helps if "identical" means identical in the way you care about. Raw HTML differs between pages that read the same, because of timestamps, session tokens and ad slots. So hash the extracted body text, not the HTML. Then normalize that text so trivial differences do not split a group.
The steps are:
- Keep only pages with a 200 status. Redirects and errors have no content of their own to compare.
- Take the main-content text, with boilerplate already removed.
- Collapse whitespace and lowercase it.
- Hash it and group by hash.
The script
import hashlib
import pandas as pd
df = pd.read_csv("crawl.csv") # columns: url, status, body_text, word_count
df = df[df["status"] == 200].copy()
def content_hash(text):
cleaned = " ".join(str(text).split()).lower()
return hashlib.sha256(cleaned.encode("utf-8")).hexdigest()
df["body_hash"] = df["body_text"].fillna("").map(content_hash)
groups = df.groupby("body_hash")["url"].agg(list)
duplicates = groups[groups.str.len() > 1]
print(duplicates)" ".join(str(text).split()) splits on any run of whitespace and rejoins with single spaces, so line breaks and double spaces no longer matter. fillna("") stops missing text from becoming the string "nan". Without it, every page with no extracted text would share one hash and look like a duplicate group.
Reading the output
Each row of duplicates is a group of URLs with the same text. Before you act on a group, check what kind of group it is.
- URL variants. The same page appears under
httpandhttps, with and without a trailing slash, or with tracking parameters. Normalize URLs first (see the earlier chapter) and these groups shrink or disappear. - Templated empties. Many pages share a hash because the extractor found only a message such as "No results found". These are thin pages, not copies of one article. Check
word_countfor the group. - True copies. Different paths serve the same article, such as a product in two categories, or a staging path left live.
You can add a column to help sort them:
dup_urls = df[df["body_hash"].isin(duplicates.index)]
print(dup_urls.groupby("body_hash")["word_count"].agg(["count", "max"]))A group with a low maximum word count points to a template problem. A group with a high one points to genuine copies.
Limits of exact hashing
- A one-word change gives a completely different hash, so near duplicates are missed. The next chapter covers those.
- Boilerplate that leaks into
body_textcan make different pages look different. A shared "related articles" block is harmless, but a rotating one breaks matching. - Aggressive cleaning can merge pages that differ only in case or spacing. That is usually fine, but case can matter in code samples.
Exercise
A crawl shows 400 URLs in one hash group, all with word_count of 12. What is the likeliest cause, and what should you check before treating them as duplicates?
Show answer
The likeliest cause is a shared template, such as an empty search or "no results" page, where the extractor found only a short message. Check which template the URLs belong to and whether the body text is just boilerplate. These are thin pages to triage by template, not copies of one article to merge.