Sub topic 9
Making links crawlable
This chapter assumes you know basic HTML, including the a element and its href attribute. It also assumes you know that a browser requests a page over HTTP and gets HTML back. The goal here is to build links that a search engine crawler can follow, and to spot markup that looks like a link to a person but not to a crawler.
How a crawler reads a link
A crawler downloads a page's HTML and looks for a elements that have an href attribute. Each href value is a URL it can add to its list of pages to fetch. The text inside the a element is read as a short description of the target page.
A link that works for a visitor can still be invisible to a crawler. A visitor's browser runs your JavaScript and responds to clicks. A crawler needs a real URL in the markup. Two ways to see the difference:
- A visitor clicks something and JavaScript changes the location. The browser handles it.
- A crawler sees no
aelement with anhref, so it has no URL to follow.
Three versions of the same link
<!-- Weak: generic anchor text -->
<a href="/guides/url-slugs/">click here</a>
<!-- Better: the anchor text describes the target page -->
<a href="/guides/url-slugs/">guide to writing URL slugs</a>
<!-- Not crawlable as a link: there is no a element with an href -->
<span onclick="location.href='/guides/url-slugs/'">guide to writing URL slugs</span>The first two are both crawlable. They differ only in how well the anchor text describes the target, which a later chapter covers. The third is not a link to a crawler. The span has no href, so the URL is hidden inside a script.
Common markup that breaks crawling
| Markup | Problem | Fix |
|---|---|---|
| `<span onclick="...">` or `<div onclick="...">` | No `a` element, no `href` | Use `<a href="/path/">` |
| `<a href="#">` with a click handler | The `href` points nowhere | Put the real URL in `href` |
| `<a href="javascript:void(0)">` | `href` is code, not a URL | Put the real URL in `href` |
| `<a>` with no `href` | Nothing to follow | Add an `href` |
| Links that appear only after a user action, such as typing in a search box | A crawler does not type or click | Also link to those pages from a normal page |
Single-page applications often fall into the first two rows. If your framework's router creates links, check that it outputs real a elements with real paths, not only click handlers.
Check your own pages
You can list the links in a page's HTML the way a crawler would. This parser collects each link's href and anchor text. It is also the starting point for spotting generic anchors during an audit.
from html.parser import HTMLParser
class LinkCollector(HTMLParser):
def __init__(self):
super().__init__()
self.links = []
self._href = None
self._text = []
def handle_starttag(self, tag, attrs):
if tag == "a":
self._href = dict(attrs).get("href")
self._text = []
def handle_data(self, data):
if self._href is not None:
self._text.append(data)
def handle_endtag(self, tag):
if tag == "a" and self._href is not None:
anchor = " ".join("".join(self._text).split())
self.links.append((self._href, anchor))
self._href = None
parser = LinkCollector()
parser.feed('<p>Read the <a href="/guides/url-slugs/">guide to URL slugs</a> or <a href="/more/">click here</a>.</p>')
print(parser.links) # [('/guides/url-slugs/', 'guide to URL slugs'), ('/more/', 'click here')]The output is a list of (href, anchor) pairs. A span with an onclick would not appear in it, which matches what a crawler sees. Run the parser on the raw HTML your server returns, not on the page after your browser has run scripts. If a link you expect is missing from that output, a crawler may miss it too.
Practical rules
- Use
awith anhreffor anything that moves a visitor to another URL. - Use a real path or full URL in
href, not#orjavascript:. - Use buttons for actions that do not navigate, such as opening a menu.
- Check the raw HTML, not only what the browser shows.
Do not rely on a sitemap alone to get a page found. A page that only a sitemap mentions has no internal links pointing to it, which makes it an orphan page. Link to it from other pages too.Exercise
A product list shows each item as <div class="card" onclick="window.location='/shoes/trail-runner/'">Trail runner shoes</div>. Why might a crawler miss these pages, and how do you fix the markup?
Show answer
The div is not an a element and has no href, so the URL exists only inside a script and the crawler sees no link to follow. Replace it with <a class="card" href="/shoes/trail-runner/">Trail runner shoes</a>. The anchor text now also describes the target page.
Exercise
You run the LinkCollector parser on a page's raw HTML and it returns an empty list, though the page shows a navigation menu in your browser. What is the likely cause?
Show answer
The menu is probably built by JavaScript after the page loads, so the raw HTML contains no a elements for it. Output the menu links as a elements with href values in the server's HTML, then run the parser again to confirm they appear.