Sub topic 5
Separating Boilerplate from Main Content
This chapter assumes you can read basic HTML structure (landmark elements such as main, nav and footer) and that you can filter and group rows in pandas. It also assumes your crawler stores the raw or rendered HTML, or at least a text field for each URL. The next chapters hash and compare page text, so this chapter covers how to produce text worth comparing.
Why boilerplate distorts your results
Boilerplate is content that repeats across many pages of a template: the header, main navigation, footer, cookie notice, sidebar, newsletter form and "free delivery" banner. It is not wrong to have it. The problem is what it does to your measurements.
- Word counts are inflated. A product page with 20 words of description and 400 words of menu and footer links reports 420 words. It looks healthy but is thin.
- Similarity is inflated. Two unrelated category pages can share 80% of their text because the template dominates. Shingle similarity (covered later) then flags them as near duplicates.
- Exact hashes break. Two pages with identical main content but different "Recently viewed" modules hash differently, so a true duplicate escapes detection.
Boilerplate can therefore push results in both directions, creating false duplicates and hiding real ones. Decide what counts as main content before you measure anything.
What counts as main content
The answer depends on the page's purpose. Use this table as a starting point.
| Region | Typical elements | Usual treatment |
|---|---|---|
| Site header and menu | `header`, `nav` | Remove |
| Footer and legal links | `footer` | Remove |
| Sidebar and widgets | `aside`, "related" blocks | Remove |
| Forms and banners | `form`, cookie and promo bars | Remove |
| Page heading and body copy | `h1`, `p`, lists inside `main` or `article` | Keep |
| Product specs, reviews, FAQs | Tables, review lists | Keep, since they are unique per page |
| Category intro text | A paragraph above a product grid | Keep |
| Product grid on a category page | Titles and prices of listed items | Judge by intent (see edge cases) |
Reviews and specifications are the usual judgment call. They are unique to the page, so they count as main content even if they sit in a tab.
Approach 1: remove regions by HTML structure
If the site uses semantic landmarks, you can strip the non-content regions and read what remains. This code uses BeautifulSoup (pip install beautifulsoup4).
from bs4 import BeautifulSoup
STRIP_TAGS = ["script", "style", "noscript", "nav", "aside", "footer", "form"]
def main_text(html):
soup = BeautifulSoup(html, "html.parser")
root = soup.find("main") or soup.find("article") or soup.body or soup
tags = list(STRIP_TAGS)
if root.name == "body":
tags.append("header") # only safe when no main/article wraps the content
for tag in root.find_all(tags):
tag.extract()
return " ".join(root.get_text(separator=" ").split())
html = (
"<body><header>Acme Shoes</header><nav>Home Shoes Sale</nav>"
"<main><h1>Trail runner</h1><p>Light shoe for rough ground.</p>"
"<aside>Related: Road runner</aside></main>"
"<footer>Copyright Acme</footer></body>"
)
print(main_text(html))
# Trail runner Light shoe for rough ground.Two details matter here. The code chooses the root element first, then removes regions inside it. A header is only stripped when there is no main or article, because an article often has its own header containing the h1. The code calls extract() rather than decompose() because a nested match, such as a form inside a nav, would otherwise be touched after its parent was destroyed.
This approach fails when the site has no landmarks, which is common with older CMS themes where everything is a div. In that case, use CSS selectors that match the site's own class names, such as div.site-footer, and keep that list per site.
Approach 2: find repeated blocks across pages
When markup gives no clue, use repetition. A block of text that appears on a large share of pages in a template is almost certainly boilerplate, whatever its tag.
from collections import Counter
def find_repeated_blocks(pages, min_share=0.5):
"""pages: dict mapping url -> list of text blocks (paragraphs, list items)."""
counts = Counter()
for blocks in pages.values():
counts.update(set(blocks))
cutoff = min_share * len(pages)
return {block for block, n in counts.items() if n >= cutoff}
def unique_text(blocks, repeated):
return " ".join(b for b in blocks if b not in repeated)
pages = {
"/shoes/red": ["Shop Sale About Contact", "Red running shoes are light.", "Free delivery over $50"],
"/shoes/blue": ["Shop Sale About Contact", "Blue running shoes are light.", "Free delivery over $50"],
"/shoes/green": ["Shop Sale About Contact", "Green trail shoes grip well.", "Free delivery over $50"],
"/shoes/black": ["Shop Sale About Contact", "Black walking shoes are soft.", "Free delivery over $50"],
}
repeated = find_repeated_blocks(pages)
print(unique_text(pages["/shoes/red"], repeated))
# Red running shoes are light.You can build the blocks from your crawl by splitting extracted text on paragraph or list-item boundaries. Some crawlers can export this directly. Others, such as Screaming Frog, let you define a content area with a CSS selector or XPath, which does the same job as Approach 1 inside the crawler.
Choose the comparison group carefully
Run this per template, not across the whole site. A block repeated on 50% of the entire site might be a blog-only sidebar. Within the product template it might appear on only 5% of pages, so a site-wide count hides it.
Group size also matters. In a group of two pages, every block appears on 100% of them, so the whole page would be classed as boilerplate. Require a minimum group size, such as 20 pages, before trusting the share cutoff.
The full workflow
Edge cases
- Listing pages. On a category page, the product grid is repeated content in one sense, since each product appears on its own page. But the grid is the purpose of the page. Measure the intro text separately from the grid, and judge the page by intent in the thin-page triage chapter.
- Content loaded by JavaScript. If the main content only appears after rendering, a raw-HTML crawl captures only the boilerplate shell. Every page then looks like a near duplicate of every other. If you see a template with unnaturally high similarity, check whether the crawl was rendered.
- Boilerplate inside the content area. Authors paste the same legal paragraph, shipping notice or "About us" blurb into hundreds of articles. Landmark removal keeps it, while repetition analysis removes it. Compare both methods if the results differ.
- Shared content that is the problem. If 500 pages share the same long supplier description, repetition analysis will label it boilerplate and remove it. That hides a real duplicate-content issue. Count how much of each page the repeated blocks make up before discarding them.
- Hidden content. Text in collapsed tabs and accordions is usually in the HTML, so it counts. Content hidden only by CSS for other reasons (such as mobile-only menus) is typically already inside
nav.
Check the extraction before using it
For each template, run these checks:
- Print the extracted text for 10 random pages and read them next to the live pages.
- Compare the median extracted word count with the raw word count. A boilerplate share of 30% to 80% is normal for ecommerce. A share near 100% means your rule removed everything. A share near 0% means it removed nothing.
- Look for extracted texts that are empty or identical across a whole template. These point to a bad selector or an unrendered crawl.
- Save the extraction rule with the results, so later recrawls use the same definition and the numbers stay comparable.
Exercise
A crawl of 3,000 product pages shows that 40 of them have identical extracted text after you strip nav and footer, but their raw HTML differs because each one shows a different "Recently viewed" list in an aside. Why would hashing the raw page text have missed these duplicates, and how would you confirm the extraction is not hiding a problem for the other 2,960 pages?
Show answer
The "Recently viewed" list is boilerplate that varies per visit, so hashes of the raw text differ even though the main content is identical. Stripping aside removes the varying part, and the 40 pages then hash the same. To confirm the extraction is safe for the other pages, compare raw and extracted word counts per template, read a random sample of extracted texts, and check that no template yields empty or near-empty text. If the 2,960 other pages keep sensible, distinct main content, the rule is not hiding anything.