AI Hints
Home›System Design Tutorial›Case Study: Video and Crawling
System Design · Lesson 105 of 120

Web Crawler

Fetching the web.

⏱ 2 min read▶ Video at 2:07:42🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

A search engine starts with a web crawler. It downloads pages, finds links in them, and follows those links to more pages. Billions of times.

Web Crawler
From the video: Web Crawler
  • Download a page
  • Find its links
  • Follow them, billions of times

The crawl loop

The crawler starts from a list of seed URLs, in a queue called the frontier. It takes a URL, downloads the page, and stores it. It extracts every link. Links it hasn't seen before go back into the frontier. And the loop continues.

  1. Frontier
  2. Download
  3. Store page
  4. Extract links
  5. New? Add to frontier
The crawl loop
From the video: The crawl loop

A tiny crawler

Here's the loop on a tiny pretend web of five pages. A set remembers every URL we've seen, so we never fetch a page twice, even though pages link back to each other. Run it. Each page is crawled exactly once, in breadth first order.

crawler.pypython
from collections import deque

web = {
    "/": ["/shop", "/about"],
    "/shop": ["/shop/mugs", "/"],
    "/about": ["/"],
    "/shop/mugs": ["/shop", "/shop/pots"],
    "/shop/pots": ["/shop/mugs"],
}
frontier, seen = deque(["/"]), {"/"}
while frontier:
    url = frontier.popleft()
    print("crawl", url)
    for link in web[url]:
        if link not in seen:
            seen.add(link)
            frontier.append(link)
Output
crawl /
crawl /shop
crawl /about
crawl /shop/mugs
crawl /shop/pots

Summary

A crawler takes URLs from a frontier, downloads pages, extracts links, and adds new ones back, remembering what it's seen. Next, crawling the real web at scale.

Key takeaways
  • Frontier queue
  • Seen set
  • Download, extract, repeat
Next up: Web Crawler at Scale — polite and parallel