By the end of this lesson you have check_page.py, a Python file that fetches one page and exits 0, 1 or 2. It exits 0 when the page answered 200 and holds your word, 1 when the site sent another status, and 2 when that exact text is missing from a 200 page. A shell or another program can act on that number without reading the page.
In lesson 1 you sent your first request by hand, read the status, and looked for a word in the content with your own eyes. That check is the part worth keeping. Checking that a fetched page is the page you meant is a step every web scraping job repeats, and a script that reports the result as a number lets the next step decide what to do without a person watching.
Why an exit code
Every program hands one small number back to whatever started it. The Bash manual says "a non-zero exit status indicates failure", and zero means success. Python sets that number through sys.exit(). Its documentation describes the argument as one that "can be an integer giving the exit status (defaulting to zero)".
One failure code is not enough here, because the two ways this check can fail send you to look in different places. A 404 says the site has no page at that address, whether from a typo or a page that was taken down, so you start with the address. A 200 without your word says that the exact text you typed is not in the content that came back. The match is case-sensitive, so pricing and Pricing are different words to it. The page may have changed, or your word may be wrong, and you find out which by reading what came back. Giving each outcome its own code lets a caller tell them apart.
The numbers carry no fixed meaning across programs. Python's documentation says that conventions for specific exit codes exist "but these are generally underdeveloped", and adds that "Unix programs generally use 2 for command line syntax errors and 1 for all other kinds of errors." This script does not follow that habit. A wrong argument count exits 1 here, and 2 is kept for the missing word. The script writes down what each number means, in the docstring at the top of the file, so a caller does not have to guess.
The script
The request body is the one from lesson 1, unchanged: the same url, request and return_format you sent with curl, now built with json.dumps and sent with urllib.request. Everything around it is new in this lesson: the two arguments, the check for the key, the status check, the word check, and the three return values that become the exit code.
The script reads your API key from the SPIDER_API_KEY environment variable, the same one lesson 1 used, and it stops with exit 1 if the variable is empty. It uses only the standard library, so there is nothing to install. Save it as check_page.py. This is the file exactly as it ran on 2026-11-20.
"""Fetch one page through the Spider API and check what came back.
python3 check_page.py URL WORD
Exit 0: the page answered 200 and holds WORD.
Exit 1: no page to check, because the key is missing or the status is not 200.
Exit 2: the page answered 200 but WORD is not in it.
"""
import json
import os
import sys
import urllib.request
API = "https://api.spider.cloud/scrape"
def main() -> int:
if len(sys.argv) != 3:
print("usage: python3 check_page.py URL WORD")
return 1
url, word = sys.argv[1], sys.argv[2]
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"}).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 check")
return 1
if word not in (page.get("content") or ""):
print(f"missing: {word!r}")
return 2
print(f"found: {word!r}")
return 0
if __name__ == "__main__":
sys.exit(main())Two lines do the work this lesson is about. main() returns a number instead of calling sys.exit in several places, so each outcome has one obvious return line. The last line passes that number to sys.exit, and Python hands it to the shell.
The codes cover the branches the script checks, and no others. If the call to the API itself fails, by timing out or by answering with an HTTP error of its own, urlopen raises an exception, and the script stops with a Python traceback before it reads any page status. An empty result list stops it the same way at page[0]. That is a failure of the call, not a status the site sent, and the script does not give it a code of its own yet.
Run it three ways
Each run below was made on 2026-11-20 with Python 3.14.7. To see the code yourself after any run, type echo $? on the next line. The Bash manual says "The exit status of the last command is available in the special parameter $?", so it prints the number the script just returned.
The first run uses the page from lesson 1 and a word it holds.
python3 check_page.py https://example.com/ 'Example Domain'url: https://example.com/
status: 200
found: 'Example Domain'That run exited 0. The page answered 200 and the word was there.
The second run asks for an address that does not exist on the same site.
python3 check_page.py https://example.com/no-such-page 'Example Domain'url: https://example.com/no-such-page
status: 404
no page to checkThat run exited 1. The site answered 404, so the script did not look for the word. Whatever the site sent with a 404 is not the page you asked for, and a match in it would have meant nothing.
The third run goes back to the lesson 1 page and asks for a word it does not hold.
python3 check_page.py https://example.com/ Pricingurl: https://example.com/
status: 200
missing: 'Pricing'That run exited 2. The address was the same as in the first run, and the word was the one thing that changed. The status alone said the fetch succeeded, and the word check reported that the exact text Pricing is nowhere in the content. That is all exit 2 tells you. It does not say why, so when you see it on a page of your own, read the content before you decide whether the page or your word is at fault.
Name your client before you use your own page
The three runs above sent the lesson 1 body as it was, to the example page. Lesson 1 asked you to send a name of your own to any other site, and the request body has a field for it, user_agent. Make this change now, before the exercise below. Replace the body = ... line with this one, and put your own bot name, page and address in place of the example ones:
body = json.dumps({
"url": url,
"request": "http",
"return_format": "markdown",
"user_agent": "examplebot/1.0 (+https://example.com/bot; bot@example.com)",
}).encode()Nothing else in the file changes, and the exit codes mean the same thing. If you later call the script many times against one site, read what robots.txt is and keep a polite request rate first.
Try it yourself
Pick a documentation page you know, and choose one heading from it as your word. Run the script on that page with that word, then run it again on the same page with a word you are sure is not there. After each run, type echo $?. You are done when the first run prints found: and echo $? shows 0, and the second prints missing: and echo $? shows 2.
Do this, avoid this
Do this
- Write what each exit code means at the top of the script, where its docstring sits.
- Check the page status before you look for the word.
- Before you trust a word, run it once on the page and once with a word you know is missing, and confirm you get 0 and then 2.
- Run the script once on an address that does not exist, and confirm it exits 1.
- Read the API key from the environment, as the script does.
Avoid this
- Exiting 0 when the word is missing.
- Using one exit code for every failure, so a caller cannot tell a wrong address from a wrong page.
- Choosing a word so common that an error page would hold it too.
- Printing the API key, even in a line meant for debugging.
- Deciding the site changed on an exit 2 before you check the spelling and case of your word.
What the script cannot tell you yet
The script answers whether one page arrived and holds your word. It does not ask whether the site wants that page fetched at all, and that is the question of the next lesson: which pages on this site may your script request?