The script you build here, list_links.py, asks for the links on one page, makes each a full address, and splits them into links on the same site and links elsewhere. On the front page of a practice site it counted 49 links, 47 distinct ones on the site and 2 elsewhere, and you will check those numbers by hand.
A crawler is a loop that reads a page, finds its links and decides which to fetch next. This lesson builds the middle of that loop and stops before the deciding. The run reads one page and follows none of its links.
From one word to every link
Lesson 5 left you with save_copy.py, which fetches one page and writes its content to a file, and check_copy.py, which looks for a word in a live page or a saved copy. The check asked one question of one page: is this word in it?
The new question is which addresses the page points to. You could dig them out of page1.md, but the API can hand them back as a list. Its reference describes the return_page_links option as one that will "Add the links found on each page. Default false." and the field it fills as "Links found on the page. Present on /links and with return_page_links." So the request gains one key, and the script reads one more field.
What the script adds
list_links.py starts from save_copy.py. Three things changed. The body sets "return_page_links": True. The import line brings in urldefrag, urljoin and urlparse from the standard library. And where save_copy.py wrote a file, the last block of main() now walks the links field, makes each link absolute, drops anything after a #, and files it under the page's own host or under elsewhere. Two Python sets do the sorting, so an address that turns up twice is counted once.
Like save_copy.py, it takes its key from SPIDER_API_KEY and imports nothing outside the standard library. This is the file exactly as it ran on 2026-11-20.
"""Fetch one page through the Spider API and list the links on it that stay on the same site.
python3 list_links.py URL
Exit 0: the page answered 200 and its links are listed.
Exit 1: the key is missing or the status is not 200.
"""
import json
import os
import sys
import urllib.request
from urllib.parse import urldefrag, urljoin, urlparse
API = "https://api.spider.cloud/scrape"
def main() -> int:
if len(sys.argv) != 2:
print("usage: python3 list_links.py URL")
return 1
url = sys.argv[1]
key = os.environ.get("SPIDER_API_KEY")
if not key:
print("set SPIDER_API_KEY first, as in lesson 1")
return 1
body = json.dumps({"url": url, "request": "http", "return_format": "markdown",
"return_page_links": True}).encode()
req = urllib.request.Request(API, data=body, headers={
"Authorization": f"Bearer {key}",
"Content-Type": "application/json",
})
with urllib.request.urlopen(req, timeout=60) as resp:
page = json.load(resp)
if isinstance(page, list):
page = page[0]
status = page.get("status")
print(f"url: {page.get('url')}")
print(f"status: {status}")
if status != 200:
print("no page to read links from")
return 1
host = urlparse(url).netloc
found = page.get("links") or []
same, other = set(), set()
for link in found:
# A link may be relative, like /page/2/, so resolve it against the page address first.
link = urldefrag(urljoin(url, link))[0]
(same if urlparse(link).netloc == host else other).add(link)
print(f"links: {len(found)} on the page, {len(same)} distinct on {host}, {len(other)} elsewhere")
for link in sorted(same):
print(f" {link}")
return 0
if __name__ == "__main__":
sys.exit(main())The recorded run went out with no user_agent, like the scripts in lessons 4 and 5. For your own run, swap the body = ... line for this one, with your bot's name and contact in place of the example.
body = json.dumps({
"url": url,
"request": "http",
"return_format": "markdown",
"return_page_links": True,
"user_agent": "examplebot/1.0 (+https://example.com/bot; bot@example.com)",
}).encode()Why each link is resolved first
A page can write a link in full, or write only the path, such as /page/2/, and leave the browser to fill in the rest. Compare the bare strings and a path with no host looks like it belongs to no site at all. RFC 3986, the standard for the syntax of addresses, says so directly: "applications should not directly compare relative references; the references should be converted to their respective target URIs before comparison." That is the job of urljoin. The Python documentation says it "uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL."
Then urldefrag cuts the fragment, the part after #. In the words of the same documentation, it will "return a modified version of url with no fragment identifier". A link to #top and a link to the page itself ask the server for the same thing, so they should count once.
The host test is strict. It compares the network location as written, so a link to www. of the same site, or to a different port, lands under elsewhere.
One more limit sits in the saved script. It uses the address you typed, url, as the page address, both as the base for urljoin and as the host to compare against. That is right when the page answers at that address, as the practice site did. If a page you pick redirects, say from http to https, from a bare host to www., or into another folder, the relative links get resolved against the wrong place and the site's own links can land under elsewhere. Open your page in a browser first. If the address bar changes, give the script the address it settles on.
Run it on the practice site
python3 list_links.py https://quotes.toscrape.com/The run below was made on 2026-11-20 with Python 3.14.7. It took one request, exited 0, and printed this.
url: https://quotes.toscrape.com/
status: 200
links: 49 on the page, 47 distinct on quotes.toscrape.com, 2 elsewhere
https://quotes.toscrape.com/
https://quotes.toscrape.com/author/Albert-Einstein
https://quotes.toscrape.com/author/Andre-Gide
https://quotes.toscrape.com/author/Eleanor-Roosevelt
https://quotes.toscrape.com/author/J-K-Rowling
https://quotes.toscrape.com/author/Jane-Austen
https://quotes.toscrape.com/author/Marilyn-Monroe
https://quotes.toscrape.com/author/Steve-Martin
https://quotes.toscrape.com/author/Thomas-A-Edison
https://quotes.toscrape.com/login
https://quotes.toscrape.com/page/2/
https://quotes.toscrape.com/tag/abilities/page/1/
https://quotes.toscrape.com/tag/adulthood/page/1/
https://quotes.toscrape.com/tag/aliteracy/page/1/
https://quotes.toscrape.com/tag/be-yourself/page/1/
https://quotes.toscrape.com/tag/books/
https://quotes.toscrape.com/tag/books/page/1/
https://quotes.toscrape.com/tag/change/page/1/
https://quotes.toscrape.com/tag/choices/page/1/
https://quotes.toscrape.com/tag/classic/page/1/
https://quotes.toscrape.com/tag/deep-thoughts/page/1/
https://quotes.toscrape.com/tag/edison/page/1/
https://quotes.toscrape.com/tag/failure/page/1/
https://quotes.toscrape.com/tag/friends/
https://quotes.toscrape.com/tag/friendship/
https://quotes.toscrape.com/tag/humor/
https://quotes.toscrape.com/tag/humor/page/1/
https://quotes.toscrape.com/tag/inspirational/
https://quotes.toscrape.com/tag/inspirational/page/1/
https://quotes.toscrape.com/tag/life/
https://quotes.toscrape.com/tag/life/page/1/
https://quotes.toscrape.com/tag/live/page/1/
https://quotes.toscrape.com/tag/love/
https://quotes.toscrape.com/tag/love/page/1/
https://quotes.toscrape.com/tag/miracle/page/1/
https://quotes.toscrape.com/tag/miracles/page/1/
https://quotes.toscrape.com/tag/misattributed-eleanor-roosevelt/page/1/
https://quotes.toscrape.com/tag/obvious/page/1/
https://quotes.toscrape.com/tag/paraphrased/page/1/
https://quotes.toscrape.com/tag/reading/
https://quotes.toscrape.com/tag/simile/
https://quotes.toscrape.com/tag/simile/page/1/
https://quotes.toscrape.com/tag/success/page/1/
https://quotes.toscrape.com/tag/thinking/page/1/
https://quotes.toscrape.com/tag/truth/
https://quotes.toscrape.com/tag/value/page/1/
https://quotes.toscrape.com/tag/world/page/1/Sort the list by hand
The saved run did the sorting in its sets and printed only the 47 on the site, in sorted order. To sort the links yourself, you need every entry of the links field, resolved, in the order the API response lists them, before any set drops a copy. Add these two lines at the end of main(), just before return 0:
for link in found:
print(f" raw: {urldefrag(urljoin(url, link))[0]}")Run the same command again, which is one more request. The lines above print as before, then one raw: line per link. Go down the raw: lines with a pen and give each one a single mark. Check for a repeat first:
- R if it repeats a
raw:line above it exactly, letter for letter. A line marked R gets no other mark. - S if its host, the part between
//and the next/, is exactlyquotes.toscrape.com. The scheme does not matter: the script compares hosts only, so anhttp://link to that host counts as the same site. - E for any other host. Write the E addresses down; the saved run counted them but never showed them.
If the page has not changed since 2026-11-20, you get 49 raw: lines and two of them are E. You should find no R. The counts line of the saved run says why: 47 on the site plus 2 elsewhere is 49, the number the response held, so on that run no two links became the same address after resolving and cutting fragments. Your run may differ if the page has changed. When your S count equals the second number on the counts line and your E count equals the third, the sort by hand and the sort by the sets agree.
Now look for the repeats an exact match cannot catch. Six tags show up twice, once as /tag/love/ and once as /tag/love/page/1/, and the same goes for books, humor, inspirational, life and simile. On a site that pages its tag lists, the first page of a tag and the bare tag address probably show the same quotes. The run does not prove it. It fetched one page and none of those addresses, and RFC 3986 warns that matching strings settle less than they seem: "URI comparison is not sufficient to determine whether two URIs identify different resources." Two strings that differ can still name one page. Fetching both addresses would give you two responses to compare, and until you do, each of the six stays a likely pair, not a known one.
Mark each of the six with a P beside both of its lines. Your hand count for this page is then 47 on the site, 2 elsewhere, no exact repeats and 6 likely pairs, all unverified. The list also holds the front page itself and /login, which a crawler of quotes has no reason to read.
That is why the crawl lessons that follow must not fetch every link blindly. Taken as they stand, these 47 addresses would include six probable second visits to pages already seen, plus a sign-in form. Each extra fetch adds one more request to the site, and lesson 4 is about spacing those out. Before any of them goes out, the site's robots.txt has a say in which paths you may fetch at all, as lesson 3 showed. Choosing what to skip is one of the habits that keeps web scraping polite once a script reads more than one page.
Try it yourself
Pick a page of your own that links to itself in more than one place, such as a long article with a table of contents, and check in a browser that it does not redirect. Run the script with the raw: lines on it, changing only the address. If you want a safe start, the practice site's second page works:
python3 list_links.py https://quotes.toscrape.com/page/2/The saved run never fetched that page, so its counts are yours to find. Mark every raw: line as before, R first and then S or E, and put a P beside any two distinct lines you suspect show one page. This time the R marks may be real: a page that links to its own sections, or to one article twice, gives repeats once the fragments are cut. You are done when your S marks equal the second number on the links: line, your E marks equal the third, and S plus E plus R equals the first.
Do this, avoid this
Do this
- Resolve every link against the page address before you count it or compare it.
- Cut the part after # so a jump within one page counts as that page.
- Check that the on-site and elsewhere counts add up to the total, and print every resolved link when they do not.
- Give the script the address a page settles on, not one that redirects.
- Write the run date beside each link list, since a page's links change.
Avoid this
- Filing a relative path like /page/2/ under no site because it has no host.
- Merging a likely pair such as /tag/love/ and /tag/love/page/1/ into one entry because the addresses look alike.
- Fetching every on-site address the moment the list prints.
- Assuming www. and the bare host are one site when the script compares them as written.
Next
You now have 47 addresses and a reason not to fetch all of them. The next lesson asks how a crawler takes that list, fetches a few pages from it at a polite pace, and keeps track of which ones it has already seen.