Sub topic 3
How Crawlers Discover and Record Links
This chapter assumes you can read simple HTML, know what a URL is, and know that search engines crawl and index pages. It explains how a crawler finds a backlink and what it stores about it. A backlink only counts once the search engine has found and recorded it.
The crawl loop
A crawler (also called a spider or bot) is a program that fetches web pages automatically. Google's crawler is called Googlebot. The crawler works in a loop:
- Take a URL from a queue of addresses to visit, called the crawl frontier.
- Fetch the page over HTTP.
- Parse the HTML and extract every link.
- Add newly found URLs to the frontier.
- Store the page and its links for indexing.
Links are how the crawler finds new pages. A page that no known page links to is hard for a crawler to find. Other sources, such as XML sitemaps, can also supply URLs. Links remain the main way the crawl spreads across the web.
What the crawler records
For each link it extracts, the crawler stores the facts needed to judge the link later:
- The source URL, which is the page containing the link.
- The target URL, taken from
href. - The anchor text between the opening and closing tags.
- The
relvalues, such asnofollow,sponsored, orugc.
From the target's point of view, each record is a backlink. The search engine stores these records as a link graph. In the graph, pages are nodes and links are directed edges. The next chapter uses this graph to explain PageRank.
Extracting links yourself
The following script does what a crawler's extraction step does. It collects the href, rel values, and anchor text for every <a> tag.
from html.parser import HTMLParser
class LinkCollector(HTMLParser):
"""Collect href, rel values, and anchor text for every <a> tag."""
def __init__(self):
super().__init__()
self.links = []
self._current = None
def handle_starttag(self, tag, attrs):
if tag == "a":
attributes = dict(attrs)
if "href" in attributes:
self._current = {
"href": attributes["href"],
"rel": (attributes.get("rel") or "").split(),
"text": "",
}
def handle_data(self, data):
if self._current is not None:
self._current["text"] += data
def handle_endtag(self, tag):
if tag == "a" and self._current is not None:
self._current["text"] = self._current["text"].strip()
self.links.append(self._current)
self._current = None
html = '<p>Read the <a href="https://example.com/guide">SEO guide</a> and <a href="https://example.org/ad" rel="sponsored nofollow">this offer</a>.</p>'
parser = LinkCollector()
parser.feed(html)
for link in parser.links:
print(link)The output is two dictionaries:
{'href': 'https://example.com/guide', 'rel': [], 'text': 'SEO guide'}
{'href': 'https://example.org/ad', 'rel': ['sponsored', 'nofollow'], 'text': 'this offer'}Three details matter:
- The script splits
relon whitespace because one attribute can hold several values. - A link with no
relgets an empty list. That is the default link, which passes ordinary link signals. - Links without an
hrefare skipped, because they point nowhere.
A real crawler also resolves relative URLs against the page's address. For example, /guide on https://example.com/blog becomes https://example.com/guide. The script above leaves them as written.
Why a link may not be found or recorded
Discovery can fail. These are the common causes:
| Cause | What happens |
|---|---|
| The page is blocked in `robots.txt` | The crawler does not fetch the page, so it never sees the links on it. |
| The link is added by JavaScript | The crawler must render the page first. Rendering is slower and can be delayed. |
| The link is not a real anchor, such as a `<span>` with a click handler | There is no `href` to extract, so no link is recorded. |
| The page has a `noindex` or error status | The page may be dropped, and its links may carry less weight over time. |
| The linking page is low priority | It may be crawled rarely, so a new link takes a long time to show up. |
Pages that crawlers visit often, such as popular news sites, have new links found quickly. Obscure pages may wait weeks.
Common errors
- Assuming every link counts the moment you publish it. A link counts only after the linking page is crawled again.
- Building navigation from JavaScript click handlers. Use real
<a href>elements so the link is visible in the HTML. - Reading only the visible text. The
relvalues change how a link is treated, so a link audit has to record them.
Exercise
A blog post contains <a href="/pricing" rel="nofollow">See plans</a> and a <span onclick="go('/docs')">Read the docs</span>. Which fields would a crawler record for each, and which of the two gives it a link to follow?
Show answer
The anchor records the source page, the target (/pricing resolved to a full URL), the anchor text "See plans", and the rel value nofollow. The span has no href, so no link is recorded and the crawler has nothing to follow.
Exercise
Why can a new backlink from a page blocked by robots.txt fail to appear in a backlink report?
Show answer
The crawler is not allowed to fetch the blocked page, so it cannot read the HTML or extract the link. The link record is never created.