# Read the run log: find the run that differs | Spider

Read the scheduled job's own log one line per run, mark any run whose rules, page count or outcome differs from the usual, and decide what to change.

Canonical: https://spider.cloud/learn/course/read-a-week-of-runs/

To read the run log, take the job's last seven runs, work out the usual rules verdict, page count and outcome, and mark every run that differs from it or whose saved robots.txt changed. A marked run is a question for you, not an answer. Read that run's files, then change the schedule, the page limit, or nothing.

At the end of this lesson you have `read_runs.py`, a script that sends no request and reads only what the job already wrote, and a mark you made on purpose to see what one looks like.

## Where the log comes from

Since [lesson 15](/learn/course/recheck-the-rules-each-run/), every run of `recheck_rules.py` writes one line to `runs/log.txt` and saves the robots.txt it read as one file per day. The request behind each line is the crawl the job sends once the rules allow it:

```python
API = "https://api.spider.cloud"
pages = call("/crawl", {"url": url, "limit": 20, "request": "http",
                        "return_format": "markdown"}, key)
```

The scheduled run on 2026-11-22 at 05:17 UTC printed these two lines and exited 0:

```text
rules: 404, 5 lines saved to runs/robots-2026-11-22.txt, start page allowed (no rules)
log: 2026-11-22T05:17:00Z  https://quotes.toscrape.com/  rules=allowed  pages=20  ok
```

Lines from earlier lessons share the file but carry no `rules=` field, so the reader below skips them.

## The reader

`read_runs.py` keeps the lines that have `rules=`, takes the last seven, and finds the most common combination of verdict, page count and outcome. Seven is a week when the job runs once a day; pass another number to read more or fewer. It prints one row per run and points at what differs. It also compares the robots file saved on each run's day with the one from the run before, because a verdict can stay `allowed` while the file under it changes. It exits 0 when every run matches, 5 when one differs and 1 when there is no log yet. Save it in the same folder as the job:

```python
"""Read the last runs of the course job's log and point at the run that differs.

    python3 read_runs.py [RUNS]

It reads runs/log.txt, keeps the lines recheck_rules.py wrote (the ones with rules=),
takes the last RUNS of them (7 by default, a week on a daily schedule), and prints one
row per run. A run is marked when its rules verdict, page count or outcome differs from
the most common one, or when the robots.txt saved on its day differs from the one saved
on the day of the run before. It sends no request and needs no key.
Exit 0: every run matches. Exit 5: at least one run differs. Exit 1: no log yet.
"""
import os
import sys
from collections import Counter

LOG = os.path.join("runs", "log.txt")


def parse(line):
    parts = line.split()
    if len(parts) < 5 or not any(p.startswith("rules=") for p in parts):
        return None
    fields = dict(p.split("=", 1) for p in parts[2:] if "=" in p)
    return {"day": parts[0][:10], "at": parts[0], "rules": fields.get("rules"),
            "pages": fields.get("pages"), "outcome": parts[-1]}


def robots(day):
    path = os.path.join("runs", f"robots-{day}.txt")
    if not os.path.exists(path):
        return None
    with open(path, encoding="utf-8") as f:
        return f.read()


def main() -> int:
    count = int(sys.argv[1]) if len(sys.argv) > 1 else 7
    if not os.path.exists(LOG):
        print("no runs/log.txt yet; run recheck_rules.py first")
        return 1
    with open(LOG, encoding="utf-8") as f:
        rows = [r for r in (parse(line.strip()) for line in f) if r][-count:]
    if not rows:
        print("no recheck_rules.py lines in runs/log.txt yet")
        return 1
    usual = Counter((r["rules"], r["pages"], r["outcome"]) for r in rows).most_common(1)[0][0]
    marked = 0
    before = None
    print(f"{len(rows)} runs, usual: rules={usual[0]} pages={usual[1]} {usual[2]}")
    for r in rows:
        why = []
        for name, want in zip(("rules", "pages", "outcome"), usual):
            if r[name] != want:
                why.append(f"{name} {r[name]}")
        text = robots(r["day"])
        if before is not None and text is not None and text != before:
            why.append("robots.txt changed")
        before = text if text is not None else before
        marked += bool(why)
        print(f"{r['at']}  rules={r['rules']}  pages={r['pages']}  {r['outcome']}"
              + (f"  <- {', '.join(why)}" if why else ""))
    return 5 if marked else 0


if __name__ == "__main__":
    sys.exit(main())
```

## Our log

The schedule sent one run on each of 2026-11-20, 2026-11-21 and 2026-11-22. We sent four more by hand on 2026-11-22, through the same job and the same key, so the reader had seven real runs to work on. This is everything it printed at 10:44 UTC that day, with exit 0:

```text
7 runs, usual: rules=allowed pages=20 ok
2026-11-20T22:37:04Z  rules=allowed  pages=20  ok
2026-11-21T05:19:25Z  rules=allowed  pages=20  ok
2026-11-22T05:17:00Z  rules=allowed  pages=20  ok
2026-11-22T10:43:39Z  rules=allowed  pages=20  ok
2026-11-22T10:43:43Z  rules=allowed  pages=20  ok
2026-11-22T10:43:48Z  rules=allowed  pages=20  ok
2026-11-22T10:44:00Z  rules=allowed  pages=20  ok
```

Every run matches, and the three robots files are the same 404 page byte for byte. Seven matching runs say the job behaves the same way each time it runs. They say nothing about a site that changes slowly, because four of the seven came minutes apart. A log from a daily schedule covers a week of the site; ours covers three days.

Why seven and not two? The usual is a vote, and the Python documentation says "Elements with equal counts are ordered in the order first encountered". With two runs that disagree, the usual is whichever came first and the other run takes the mark, right or wrong. With seven, one odd run is outvoted and the mark lands on it.

## What a mark asks you to do

Each kind of mark points at different files.

- **pages lower than usual.** Open that run's CSV in `runs/`. If the missing rows are pages the site removed, change nothing. If the crawl stopped early, read that run's own output before you touch the limit.
- **outcome skipped or failed.** A skipped line means the rules check stood the crawl aside, as lesson 15 made it do. Read that day's robots file. A failed line points at that run's output, not the schedule.
- **robots.txt changed.** RFC 9309 says "Crawlers SHOULD NOT use the cached version for more than 24 hours, unless the robots.txt file is unreachable", which is why the job reads the file every run. When the file changes, diff the two days, then reread the site's terms before the next run.
- **rules changed.** The start page went from allowed to blocked or unreachable. Leave the job skipping. Do not edit the rules check to get the crawl back.

A change to the schedule is rarely the answer. Run less often only when the pages change less often than you crawl them, which the CSVs show when they come out the same run after run.

## Try it yourself: make a mark on purpose

Copy your runs folder to a scratch folder so the real log stays as it is, then change the last page count in the copy:

```bash
mkdir -p /tmp/runs-copy && cp -R runs read_runs.py /tmp/runs-copy/
cd /tmp/runs-copy
sed -i.bak '$ s/pages=20/pages=5/' runs/log.txt
python3 read_runs.py
echo $?
```

On a copy of our log the reader printed this and exited 5:

```text
7 runs, usual: rules=allowed pages=20 ok
2026-11-20T22:37:04Z  rules=allowed  pages=20  ok
2026-11-21T05:19:25Z  rules=allowed  pages=20  ok
2026-11-22T05:17:00Z  rules=allowed  pages=20  ok
2026-11-22T10:43:39Z  rules=allowed  pages=20  ok
2026-11-22T10:43:43Z  rules=allowed  pages=20  ok
2026-11-22T10:43:48Z  rules=allowed  pages=20  ok
2026-11-22T10:44:00Z  rules=allowed  pages=5  ok  <- pages 5
```

You are done when the last row ends in `<- pages 5` and `echo $?` prints 5. Now run `python3 read_runs.py 2` on the same copy. With two runs the counts tie, the first run wins the vote, and the edited run still takes the mark. Edit the second to last line instead and run it with 2 again: the mark moves to the run you did not touch. That is the tie rule at work, and the reason to read seven runs before you act on a single mark.

## Do this, avoid this

### Do this

- Read the log once a week, one line per run, before you change anything about the job.
- Open the marked run's CSV, robots file and output before you decide what the mark means.
- Reread the site's terms and its robots.txt when the saved file changes, even if the verdict did not.
- Read enough runs that one odd run is outvoted before you trust the usual.

### Avoid this

- Raising the page limit or the schedule because one run came back short.
- Editing the rules check so a skipped run crawls again.
- Testing the reader on the real log; edit a copy.
- Calling a site stable over days from runs sent minutes apart.

## Next

The job has run unattended on a schedule, checked the rules each time, and left a log you can read in a minute. The course so far fetched every page as plain HTTP. In [lesson 17](/learn/course/when-a-page-needs-a-browser/) you meet a page whose plain markdown answer comes back without its quotes, and learn how to tell when a browser is worth it. The [static vs rendered](/learn/static-vs-rendered/) page reads the raw body of that same page and finds the records in a script. The [web scraping](/web-scraping/) guide puts the whole job in context.

## Sources

1. [Python documentation, collections, Counter.most_common](https://docs.python.org/3/library/collections.html) Read November 21, 2026
2. [RFC 9309, Robots Exclusion Protocol, IETF, September 2022](https://www.rfc-editor.org/rfc/rfc9309.html) Read November 21, 2026
