Sub topic 5
Discovering and normalizing URLs
This chapter assumes you know how a crawler fetches a page and extracts <a href> links from HTML, as covered in the previous chapters. You should also be able to read short Python snippets.
Where URLs come from
A crawler cannot fetch a page it has not heard of. It learns about URLs from a few sources:
- Links on pages it has already fetched.
- Sitemaps that site owners submit or list in robots.txt.
- Redirect targets, from the
Locationheader of a 301 or 302 response. - URLs it saw on earlier crawls.
Links are the main source. Each fetched page adds new candidates, so the known set of URLs grows outward from the seed URLs.
Why normalization matters
The same page can be written many ways. These strings can all point to one resource:
https://Example.com:443/ahttps://example.com/a#section-2https://example.com/a
If the crawler treats them as different URLs, it fetches the page several times. That wastes crawl budget and creates duplicates. Normalization turns equivalent spellings into one form before the "already seen" check.
Safe normalization steps
Some changes never alter which resource a URL names:
- Resolve relative links against the page's URL. Without this,
../cis meaningless. - Lowercase the scheme and host. Both are case-insensitive.
- Remove default ports (80 for http, 443 for https).
- Remove the fragment. The part after
#is never sent to the server. - Use
/as the path when the path is empty.
from urllib.parse import urljoin, urldefrag, urlsplit, urlunsplit
def normalize(base, href):
absolute, _ = urldefrag(urljoin(base, href))
parts = urlsplit(absolute)
scheme = parts.scheme.lower()
host = (parts.hostname or "").lower()
port = parts.port
if port and (scheme, port) not in {("http", 80), ("https", 443)}:
host = f"{host}:{port}"
path = parts.path or "/"
return urlunsplit((scheme, host, path, parts.query, ""))
print(normalize("https://Example.com/a/b.html", "../c?x=1#top"))
print(normalize("https://example.com/", "HTTPS://EXAMPLE.com:443"))The output is https://example.com/c?x=1 and then https://example.com/.
Risky normalization
Other changes depend on how the server behaves, so a crawler cannot assume they are safe.
- Paths are case-sensitive on many servers.
/Aboutand/aboutmay be different pages. - A trailing slash can matter.
/docsand/docs/may return different content. - Query parameter order usually does not matter, but the crawler cannot be sure. Sorting parameters is a common heuristic.
- Tracking parameters such as
utm_sourceand session IDs usually do not change content. Removing them is a rule per site, not a universal one.
Crawler traps
Some sites generate endless URLs, such as a calendar with a "next month" link that never ends. Normalization cannot fix this. Crawlers add limits on URL length, path depth, and the number of URLs per host. They also watch for repeating path segments such as /a/b/a/b/a/b.
Exercise
A crawler sees HTTP://Shop.example.com:80/Items?id=5#reviews and http://shop.example.com/Items?id=5. After the safe steps from this chapter, are they the same URL? Which part of the path did you not change, and why?
Show answer
Yes. Lowercasing the scheme and host, removing default port 80, and dropping the fragment #reviews give http://shop.example.com/Items?id=5 for both. The path /Items keeps its capital letter because paths can be case-sensitive on the server.