Sub topic 5
Separating boilerplate from main content
This chapter assumes you can read basic HTML (head, body, headings, links), can load a crawl export into pandas, and know what a word count column in a crawler means. You will learn why a crawler's raw text can mislead you, and how to strip repeated page furniture before you measure anything.
Why boilerplate distorts your numbers
Boilerplate is the text that repeats across many pages of a site: the header, navigation menu, footer, cookie notice, sidebar and "related links" blocks. Main content is the part that is specific to the page and answers the visitor's reason for coming.
Crawlers often count every word in the body element. That causes two opposite errors:
- False uniqueness. A thin page with 40 words of real content still shows 400 words, because the menu and footer add 360. It passes your word-count filter.
- False duplication. Two different product pages look nearly identical to a similarity measure because 90 percent of their text is the shared template.
Both errors come from measuring the whole page when you should measure the main content. The later chapters on hashing, shingles and thin page metrics all depend on this step.
Three ways to find the main content
- Use a content area setting in the crawler. Screaming Frog and Sitebulb let you include or exclude elements by CSS selector or HTML tag, for example only
mainorarticle. This is the most reliable option when the site's markup is consistent. - Exclude known regions in your own script. Remove
nav,header,footerandasideelements, and any blocks you identify by class name. - Detect repeated text statistically. Split text into lines or blocks and drop any block that appears on a large share of pages. This works without knowing the markup.
The first two depend on the site using sensible semantic tags. Many sites use only div elements with arbitrary class names, so inspect a few templates in a browser before you trust a selector.
Worked example: removing repeated blocks
The script below parses HTML with BeautifulSoup, removes the usual furniture elements, splits what is left into blocks, and drops any block that appears on more than half of the pages. It then reports how much of each page was boilerplate.
from collections import Counter
from bs4 import BeautifulSoup
FURNITURE = ["nav", "header", "footer", "aside", "script", "style"]
def blocks(html):
soup = BeautifulSoup(html, "html.parser")
for tag in soup(FURNITURE):
tag.decompose()
text = soup.get_text("\n")
lines = (" ".join(line.split()) for line in text.splitlines())
return [line for line in lines if line]
pages = {
"/shoes/red": "<body><nav>Home Shop About</nav><main><h1>Red shoes</h1><p>Light road shoes.</p><p>Free returns within 30 days.</p></main></body>",
"/shoes/blue": "<body><nav>Home Shop About</nav><main><h1>Blue shoes</h1><p>Trail shoes with grip.</p><p>Free returns within 30 days.</p></main></body>",
"/shoes/green": "<body><nav>Home Shop About</nav><main><h1>Green shoes</h1><p>Wide fit walking shoes.</p><p>Free returns within 30 days.</p></main></body>",
}
page_blocks = {url: blocks(html) for url, html in pages.items()}
counts = Counter(b for bl in page_blocks.values() for b in set(bl))
limit = len(pages) * 0.5
for url, bl in page_blocks.items():
main = [b for b in bl if counts[b] <= limit]
print(url, "words before:", sum(len(b.split()) for b in bl),
"after:", sum(len(b.split()) for b in main))The "Free returns" line sits inside main, but it appears on every page, so the statistical step removes it. Only the heading and description remain, which is the text that distinguishes each page.
Gotchas
- A threshold that is too low removes real content. On a small section of five pages, a sentence shared by three of them is above 50 percent but may be legitimate shared copy. Run the repeated-block step per template, not across the whole site.
- Shared text is sometimes the problem. If two pages are identical except for the heading, the repeated description is the duplication you are hunting. Keep the before and after counts so you can see both.
- Content loaded by JavaScript does not exist in a plain HTML crawl. If main content is missing, check whether the crawler rendered the page.
Exercise
A category page shows 520 words in your crawl, but 480 of them come from the menu, footer and a shared delivery notice. Which error does this create, and what should you do before applying a 150-word thin page cutoff?
Show answer
It creates false uniqueness: the page looks substantial but has only about 40 words of its own content. Remove the boilerplate, by a content area selector or by dropping repeated blocks per template, then recalculate the word count and apply the cutoff to the main content.