Know when your scraper runs fine but returns nothing

Updated 4 min read

On this page
  1. The problem
  2. What goes wrong silently
  3. Setup
  4. Recommended settings
  5. Why the snippets send only 2 KB
  6. Related
  7. FAQ

Your scraper sends Stallbell the start of what it found, and Stallbell emails you when that's empty, when it looks like a block page, or when the run fails, hangs or doesn't happen.

The problem

A scraper runs every 6 hours and feeds a price list. The shop changes its HTML, the selectors match nothing, and the scraper exits 0 with an empty file. Or the site starts serving a "Just a moment..." page and the parser finds no products in it. Cron sees a success, so nobody hears about it until a customer asks why the prices haven't changed in a week.

This page is a ready-made setup for one scenario.

What goes wrong silently

What Stallbell catches
What happensWhat Stallbell seesAlert
Selectors match nothing after a site change, and the scraper exits 0Output of 0 bytesEmpty output
The site serves a block page or captcha (with Output must contain set)Output without your marker stringEmpty output
The scraper crashes or exits non-zero (a 403, a parse error)A fail ping with the exit code or messageFailed run
The scraper hangs on a requestA start ping with no finishStuck run
Cron stops running itNo ping by the expected time plus graceMissed run
The next run finds dataA good finish after the incidentRecovered

Creates a monitor called scraper-empty-after-site-change with the settings below. You can change everything later.

Setup

Send a start ping, run the scrape, then send the first 2 KB of the result as output. An empty result is the signal, so the important part is that "nothing found" really arrives as empty, not as [].

Python
import json
import time
import urllib.request

PING = "https://stallbell.com/p/TOKEN"

def ping(path, **fields):
    req = urllib.request.Request(
        PING + path,
        data=json.dumps(fields).encode(),
        headers={"content-type": "application/json"},
        method="POST",
    )
    try:
        urllib.request.urlopen(req, timeout=10).read()
    except Exception as exc:  # never let monitoring break the job
        print(f"Stallbell ping failed: {exc}")

def scrape():
    ...  # your scraper: return a list of rows (dicts)

run_id = f"scrape-{int(time.time())}"
ping("/start", run_id=run_id)
try:
    items = scrape()
except Exception as exc:
    ping("/fail", run_id=run_id, msg=str(exc)[:500])
    raise
rows = "\n".join(json.dumps(item) for item in items)
ping("/ok", run_id=run_id, msg=f"{len(items)} rows", output=rows.encode()[:2048].decode(errors="ignore"))
Shell
#!/bin/sh
# scrape-and-report: run the scraper, then send the first 2 KB of its result to Stallbell.
URL="https://stallbell.com/p/TOKEN"
OUT="/tmp/products.jsonl"

curl -fsS -m 10 --retry 3 -o /dev/null "$URL/start"
python3 /opt/scraper/scrape.py > "$OUT"
code=$?

n=$(head -c 2048 "$OUT" | wc -c)
head -c 2048 "$OUT" | curl -fsS -m 10 --retry 3 -o /dev/null \
  --data-urlencode "output@-" "$URL/$code?output_len=$n"
exit "$code"

Python: the rows go out one JSON object per line, so no rows means an empty string. Don't send json.dumps(items) for the whole list: an empty list is [], which is two characters and not empty. The msg (for example 2 rows) shows under Recent runs; click a run to see it, with the first 500 characters of the output, in the Run drawer.

Shell: use this when the scraper is a separate program that writes its result to a file. The output_len is there because curl leaves out an empty form field, and without it an empty file would arrive as "no output" instead of "empty output". It's the size of the 2 KB being sent, not of the whole file (see below).

Recommended settings
FieldValueWhy
KindScheduled jobA label only. The scraper runs on a schedule.
How it runsOn a fixed intervalThe scraper runs every few hours, so a fixed interval matches it.
Every6 hoursEvery 6 hours. Change it to match how often your scraper runs.
Grace period30 minutes30 minutes covers a slow crawl that starts late.
Failures in a row1A crash or a block is worth hearing about on the first run.
Stuck after30 minutes30 minutes. A crawl that runs much longer than usual is usually hung on a request.
Flag empty outputOnThe snippet sends the scraped rows as output, so a run that found nothing raises Empty output.

Optional: catch block pages. Under Output must contain, enter a string every good result has, such as "price" (with the quotes, so it matches a JSON key). A block page or captcha without it raises Empty output too. The template leaves this blank, because the right string depends on your data.

Why the snippets send only 2 KB

Stallbell stores the first 2,048 bytes of output. If the size it's told is bigger than that, it treats the output as cut and skips Output must contain, because the marker could be further down. In our test, a 3,898-byte block page reported with its full file size passed the check. Reported with the size of the 2 KB actually sent, the same page raised Empty output. So the snippets send the first 2 KB and its own size. Put the most typical rows first if your output has a header.

FAQ

My scraper exits 0 when it finds nothing. Will Stallbell still notice?

Yes, if you send the result as output. An empty result raises Empty output whatever the exit code.

Can it tell a block page from real results?

Set Output must contain to a string every good result has, such as "price". A captcha or block page without it raises the same warning.

How much of the result does Stallbell look at?

The first 2 KB you send. The snippets send only that much, so the content check is never skipped for size.

Can it alert when I get fewer rows than usual?

Not by itself. Stallbell doesn't compare runs. Check the count in your script and send /fail with a message when it's too low.