Know when your scraper runs fine but returns nothing
On this page
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 happens | What Stallbell sees | Alert |
|---|---|---|
| Selectors match nothing after a site change, and the scraper exits 0 | Output of 0 bytes | Empty output |
| The site serves a block page or captcha (with Output must contain set) | Output without your marker string | Empty output |
| The scraper crashes or exits non-zero (a 403, a parse error) | A fail ping with the exit code or message | Failed run |
| The scraper hangs on a request | A start ping with no finish | Stuck run |
| Cron stops running it | No ping by the expected time plus grace | Missed run |
| The next run finds data | A good finish after the incident | Recovered |
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 [].
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"))#!/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
| Field | Value | Why |
|---|---|---|
| Kind | Scheduled job | A label only. The scraper runs on a schedule. |
| How it runs | On a fixed interval | The scraper runs every few hours, so a fixed interval matches it. |
| Every | 6 hours | Every 6 hours. Change it to match how often your scraper runs. |
| Grace period | 30 minutes | 30 minutes covers a slow crawl that starts late. |
| Failures in a row | 1 | A crash or a block is worth hearing about on the first run. |
| Stuck after | 30 minutes | 30 minutes. A crawl that runs much longer than usual is usually hung on a request. |
| Flag empty output | On | The 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.
Related
- Docs: check-in fields
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.