8/28
Part 3 of 6 · Clean linksLearn and do
Clean every link before you compare it
The visited set compares strings, and pages write the same link in many ways.
the link = ../c#top. Found on the page http://example.com/docs/a.html.
The same page shows up under many spellings, and seen compares strings. So every link goes through the same three steps before you compare it to anything.
Resolve a relative link against its page
from urllib.parse import urljoin
page = "http://example.com/docs/a.html"
for href in ["b.html", "/b", "../c", "?page=2", "//other.com/x",
"mailto:me@example.com"]:
print(f"{href:23} {urljoin(page, href)}")
The page is http://example.com/docs/a.html. What does urljoin(page, "../c") give?
Pick one to see what it prints.
Pick an answer above to see the rest