Sub topic 3
Extracting and Tokenizing Page Text
Assume you understand HTTP responses, basic HTML, and short Python programs. This chapter follows a page from visible text to a searchable index.
From page text to tokens
After fetching a page, a search engine extracts useful text from its HTML. It may ignore navigation, scripts, styles, and repeated boilerplate. It then tokenizes the text. Tokenization splits text into searchable units called tokens, usually words or numbers.
A tokenizer often lowercases text and removes punctuation. Real systems also handle languages, spelling, stemming, and synonyms.
import re
def tokenize(text):
return re.findall(r"[a-z0-9]+", text.lower())
print(tokenize("Crawlers find Web pages!"))
# ['crawlers', 'find', 'web', 'pages']Building an inverted index
An inverted index maps each token to the documents that contain it. This lets a search system find matching documents without scanning every page for every query.
from collections import defaultdict
documents = {
1: "Search engines crawl web pages",
2: "Crawlers follow links to find web pages",
3: "An index maps words to pages",
}
index = defaultdict(set)
for document_id, text in documents.items():
for token in tokenize(text):
index[token].add(document_id)
print(sorted(index["pages"]))
# [1, 2, 3]A production index also stores positions, fields, language information, links, and ranking signals.
Controlling indexing
robots.txt controls whether a crawler may fetch a URL. It does not directly remove already indexed content. A noindex directive asks the search engine not to include a page in its index, but the crawler must fetch the page to see it.
Recrawling and serving results
Search engines recrawl pages according to estimated change frequency, importance, server capacity, and crawl limits. A sitemap can help discovery, but it does not guarantee immediate crawling or indexing. Once processed, the index supplies candidate documents for a query. Ranking then orders those candidates using relevance, quality, freshness, and other signals.
Diagnosing missing pages
Check likely causes in this order:
- The URL is not discovered through links or a sitemap.
robots.txtprevents fetching it.- The server returns an error, redirect, or unsuitable content type.
- JavaScript is required before useful content appears.
- A canonical URL points indexing to another page.
- A
noindexdirective prevents inclusion. - The page is new, low priority, or waiting for recrawling.
Exercise
A page is absent from search results, and its robots.txt entry allows crawling. What should you check next?
Show answer
Fetch the page and inspect its status, redirects, rendered content, canonical URL, and noindex directives.