Metadata-Version: 2.4
Name: falseok
Version: 0.3.0
Summary: Catch the 200 OK that isn't: spot block pages and silently broken scraped data.
Author: Benchmarked
License: MIT
Project-URL: Homepage, https://benchmarked.website
Keywords: scraping,web-scraping,captcha,data-quality,monitoring,soft-404
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/hero-dark.svg">
    <img src="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/hero-light.svg" alt="falseok: catch the 200 OK that isn't" width="880">
  </picture>
</p>

**Your scraper didn't fail. It succeeded at saving a captcha.**

`falseok` tells you when a scraped page that came back `200 OK` is really a block, a login wall or an empty shell,
and when today's dataset quietly broke.

```bash
pip install falseok
falseok page product.html --status 200
falseok data today.json --vs yesterday.json
```

Pure Python, zero dependencies, works with whatever fetches your pages. It recognises blocks. It never tries to get
around them.

## Twelve tools, the same fresh pages

<!-- bench:start -->
<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/bench-dark.svg">
    <img src="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/bench-light.svg" alt="Twelve tools on the same 982 fresh pages: falseok gave a wrong verdict on 3.2% of them, the next best tool on 9.9%; falseok caught 97.4% of the anti-bot blocks and 73.1% of the unusable 200 OK pages, and flagged 0.5% of the real pages" width="880">
  </picture>
</p>

982 pages drawn at random from 40,432 fresh pages on 30,066 sites in 38 languages; no rule of any falseok version was written or tuned on them. Labels come from three independent reviewers; a page counts when at least two of them gave it the same label. The 144 hardest pages of the sample were then re-checked by hand and on screenshots, and that label wins. Every tool judged the same bytes, with the status code and the response headers. 982 pages got a label: 656 real, 117 anti-bot blocks, 141 other refusals, 68 empty. 104 of the 326 unusable pages came back without an error status; the table calls them "200 OK". A wrong verdict is an unusable page let through or a real page flagged, out of all 982.

| tool | wrong verdicts | blocks caught | unusable "200 OK" caught | real pages flagged | other refusals caught | empty pages caught | ms per page |
|---|---:|---:|---:|---:|---:|---:|---:|
| **falseok** | **3.2%** | **97.4%** | **73.1%** | 0.5% | **90.1%** | **83.8%** | 1.59 |
| *Built for the same job* | | | | | | | |
| a Rust scraper's anti-bot classifier | 16.8% | 94.9% | 33.7% | 0.2% | 16.3% | 41.2% | 1.68 |
| the blocked-page detector of a popular Python crawler | 16.8% | 94.9% | 33.7% | 0.2% | 16.3% | 41.2% | 3.15 |
| a new Python response classifier | 23.5% | 65.0% | 20.2% | 0.3% | 2.1% | 26.5% | 49.5 |
| the most used anti-bot detector, JavaScript | 25.3% | 63.2% | 3.8% | **0.0%** | 0.7% | 4.4% | 2.21 |
| a Rust challenge detector | 39.6% | 64.1% | 14.4% | 25.9% | 17.0% | 11.8% | 0.17 |
| *Rules inside scraping frameworks* | | | | | | | |
| the ban rule of the most used proxy-rotation add-on | 9.9% | 96.6% | 6.7% | **0.0%** | 78.0% | 8.8% | 0.12 |
| the blocked-request rule of a major crawling framework | 21.9% | 94.0% | 0.0% | **0.0%** | 0.7% | 0.0% | 22.7 |
| the challenge checks of the most used Cloudflare library | 33.2% | 0.0% | 0.0% | **0.0%** | 0.0% | 0.0% | 0.02 |
| *One-line checks* | | | | | | | |
| status-code check (4xx/5xx = bad) | 10.6% | 95.7% | 0.0% | **0.0%** | 78.0% | 0.0% | <0.01 |
| page-size check (under 10 KB = bad) | 14.6% | 92.3% | 42.3% | 1.5% | 38.3% | 45.6% | <0.01 |
| keyword search (captcha, blocked, access denied...) | 63.6% | 83.8% | 51.0% | 77.3% | 51.1% | 55.9% | 2.89 |

falseok gave a wrong verdict on 3.2% of the labelled pages; the next best tool, the ban rule of the most used proxy-rotation add-on, on 9.9%: **3.1 times fewer** wrong verdicts. Bold is the best value in a column. The one-line checks are the fastest because they read almost nothing: a status code, a length.

Some of these tools were built for a narrower job (only Cloudflare challenges, only status codes, only anti-bot vendors); the [benchmark](https://github.com/benchmarked-hq/falseok/blob/main/docs/benchmark.md) quotes what each one claims to catch, with 95% confidence intervals.

"Other refusals" are pages not found, server errors, login-only pages and the like. Most of them carry an error status, which is why a plain status check does well there (78.0%) and catches no empty page at all.

**Where falseok loses: real pages flagged.** It flagged 3 of 656 real pages (0.5%). Five tools flagged none, two more flagged a single page. The 3 were short real pages (33, 57 and 961 characters of visible text) called empty. None of them was called an anti-bot block. The tools with zero false alarms judge only the status code or a few known signatures, or decline to judge many pages, and they catch 0 to 7 of the 104 unusable pages that came back without an error status; falseok caught 76.

**What it missed:** 3 of the 117 blocks, all served with status `200`.

**Speed:** about 1.4 times faster than the most used JavaScript detector, level with the Rust classifier, about 31.2 times faster than the new Python classifier. The quickest checks (the Rust challenge detector, the proxy add-on's ban rule, the Cloudflare checks, status code, page size) are faster still, because they read almost nothing or stop at the first match. Next to a download of 500 ms or more, under 2 ms is noise.
<!-- bench:end -->

The table above was measured with 0.3.0, code frozen, on pages no version was tuned on. How 0.3 compares with 0.1 and 0.2 on the same fresh pages and made-up addresses:

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/versions-dark.svg">
    <img src="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/versions-light.svg" alt="Three versions on the same fresh pages (982 labelled pages and 5,315 made-up addresses): 0.3 checks a page in 1.68 ms (0.1: 15.4 ms, 0.2: 2.15 ms), catches 74.6% of made-up addresses that answer 200 OK (0.1: 64.2%), gets 96.8% of everyday pages right (0.1: 96.4%), and checks 2,000 files 3.6 times faster on all cores" width="880">
  </picture>
</p>

## New in 0.3

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/new03-dark.svg">
    <img src="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/new03-light.svg" alt="New in 0.3: where the request ended up (url, final_url), what to do next (action, confidence, retry_after), page facts, your own rules, many pages at once (check_pages, --jobs)" width="880">
  </picture>
</p>

## More than a block detector

<!-- more:start -->
<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/more-dark.svg">
    <img src="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/more-light.svg" alt="More than a block detector: page not found 92.4% of 5,315 made-up addresses, empty shells 83.8%, block markers in the response headers of 4 anti-bot services, 44 languages, 1,020 of 1,020 broken dataset days caught" width="880">
  </picture>
</p>

- **Page not found:** 4,910 of 5,315 made-up addresses on real sites (92.4%), including 173 of the 232 that answered `200 OK` at the same address; 5,158 (97.0%) when you also pass `url` and `final_url`.
- **Empty shells:** 57 of 68 pages that loaded a menu and a footer and nothing else.
- **Response headers:** pass them (`--headers`, or `headers=` in Python) and it reads the anti-bot service's own
  block marker and names the vendor (4 services so far).
- **44 languages:** "page not found", "access denied" and the other wording sites write themselves, each phrase
  taken from a page that really came back.
- **Datasets:** on four real scraped datasets, it caught **1,020 of 1,020** deliberately broken days (lost rows,
  emptied or vanished fields, frozen values, type changes, duplicates, block pages saved as rows) and stayed quiet on
  **200 of 200** healthy ones. A row-count alert, what most pipelines have, caught 240.

Three things we learned the hard way:

- **Status codes lie.** 104 of the 326 unusable pages in the benchmark came back without an error status. A status check calls all of them a success.
- **Grepping for "captcha" flags 77% of real pages** (507 of 656). Anti-bot scripts, signup captchas and cookie
  banners load on normal pages too.
- **Block pages are short. Real pages aren't.** The block pages we measured carry 0 to ~2,000 characters of visible
  text. That one fact does most of the work, and it is also where falseok's few false alarms come from.
<!-- more:end -->

## Check pages

```bash
falseok page product.html
falseok page pages/*.html --status 200
falseok page page.html --headers headers.txt   # the response headers, as `curl -D headers.txt` saves them
falseok page page.html --json
```

### Where the request ended up

Give the address you asked for and the one you landed on after redirects (`--final-url` may be relative). A deep
link that ended on a sign-in, consent, error or home page, on a captcha or challenge address, or on a search that
lost its words is flagged even when the page itself looks fine. Without them nothing changes.

```bash
falseok page page.html --url https://example.com/item/123 --final-url "/login?next=/item/123"   # login_wall
```

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/verdicts-dark.svg">
    <img src="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/verdicts-light.svg" alt="Every page gets one verdict: ok, not_html, challenge, captcha, rate_limited, login_wall, consent_wall, soft_block, soft_404, empty_shell, http_error" width="880">
  </picture>
</p>

<details>
<summary>The verdicts as text</summary>

| verdict | what it means |
|---|---|
| `ok` | no block signal found |
| `not_html` | binary or compressed data, not a page |
| `challenge` | an anti-bot check page (the vendor is named when the HTML shows it) |
| `captcha` | a captcha in place of the content |
| `rate_limited` | "too many requests", or HTTP 429 |
| `login_wall` | you need to sign in to see this |
| `consent_wall` | a consent page in place of the content |
| `soft_block` | "access denied", "continue shopping", "something went wrong" served as 200 |
| `soft_404` | 200, but the page says it does not exist |
| `empty_shell` | empty response, a saved redirect, or a JavaScript app that never rendered |
| `http_error` | you passed `--status` 4xx/5xx and no other signal was found |

</details>

### What to do next

Every result also carries an `action`: `keep`, `render_js` (the content needs a browser to appear), `retry`,
`backoff` (wait, then retry slower; `retry_after` holds the seconds when the server sent `Retry-After`), `drop`,
`decompress` (the body is still packed) or `stop` (the site refuses this client or wants a person; falseok does not
suggest ways around a block). `confidence` is `high`, `medium` or `low`: a vendor, a response header or the status
is sure, a page's wording less so, a guess from the page's shape least. The table and the rules are in
[docs/verdicts.md](https://github.com/benchmarked-hq/falseok/blob/main/docs/verdicts.md).

```
challenge [Cloudflare]: cf-mitigated-header (header: "cf-mitigated: challenge") -> stop (high confidence)
```

### Adjust the rules

Add your own rules on top of the bundled ones (same JSON format; one with an existing id replaces it), turn rule or
signal ids off, or accept a category you expect, such as a login wall when you scrape behind a login:

```bash
falseok page page.html --extra-signatures my_rules.json --disable enable-js --allow login_wall
```

```python
r = check_page(html, signatures=load_signatures(extra_signatures="my_rules.json"), allow_categories=["login_wall"])
```

Details in [docs/signatures.md](https://github.com/benchmarked-hq/falseok/blob/main/docs/signatures.md#adjust-the-rules).

Many pages at once: `falseok page pages/*.html --jobs 0` uses every CPU core; the answers and their order are the
same as with one. In Python, `falseok.check_pages(items, jobs=None)` takes html (or dicts of `check_page` arguments)
and returns a list of results in the same order.

```python
results = falseok.check_pages([{"html": body, "status": 200} for body in bodies], jobs=4)
```

## Check data

```bash
falseok data today.json --vs yesterday.json
```

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/terminal-dark.svg">
    <img src="https://raw.githubusercontent.com/benchmarked-hq/falseok/main/assets/terminal-light.svg" alt="falseok data output: 131 rows instead of 200, 10 duplicates, a block page saved as a row, salary 62% empty" width="880">
  </picture>
</p>

Reads JSON, JSON Lines, CSV and TSV, including what Excel saves on Windows and European semicolon CSVs. Finds lost
rows, fields that went empty, disappeared or froze on one value, changed types, duplicates, and block-page text that
got parsed as data. Without `--vs` it checks the file on its own.

## In a cron job or CI

Exit code `0` = fine, `1` = something is wrong, `2` = the input could not be read.

```bash
falseok data out/today.jsonl --vs out/last_good.jsonl && cp out/today.jsonl out/last_good.jsonl
```

## From Python

```python
import falseok

r = falseok.check_page(html, status=200)   # html can be text or the raw bytes you received
r = falseok.check_page(resp.content, status=resp.status_code, headers=resp.headers)   # headers are optional
r = falseok.check_page(resp.content, url=asked_url, final_url=resp.url)   # where the redirects ended, optional
if not r.ok:
    print(r.verdict, r.reason)   # captcha robot-check-title (title: "Robot Check")
print(r.action, r.confidence, r.retry_after)   # keep / render_js / retry / backoff / drop / decompress / stop
print(r.content_kind, r.canonical_url, r.meta_refresh, r.script_count)   # html / document / data / compressed / binary / empty

report = falseok.compare(rows_today, rows_yesterday)
for issue in report.issues:
    print(issue.kind, issue.message)
```

## How hard we tried to break it

- **A stress suite** written to break it: random bytes, 50 MB pages, unclosed tags, every rule's wording hidden in
  long real pages, Excel CSVs, JSON nested three levels down, a million rows. The first run found 107 failures. All
  fixed, all still in the suite, which now holds 2,462 tests.
- **140,000 random inputs** from property-based testing. Nothing broke.
- **Five fresh sets of pages** from sites it had never seen. Every set that showed misses we then fixed became a
  working set, so we don't quote it as proof (the 0.2.0 benchmark set is one of them now). The benchmark above is the
  untouched half of a random sample from the fifth set (40,432 pages), scored with the code frozen.
- **Linux, macOS and Windows, Python 3.10 to 3.14**, on every change.

The whole story, failures included: [docs/testing.md](https://github.com/benchmarked-hq/falseok/blob/main/docs/testing.md).

## Where the signatures come from

Every rule lives in one file, [`src/falseok/signatures.json`](https://github.com/benchmarked-hq/falseok/blob/main/src/falseok/signatures.json): a pattern, where to
look (title, visible text or raw HTML), and the category. They come from block pages our own production scrapers
received with `200 OK`, and from the live tests above. The test pages in [`tests/pages`](https://github.com/benchmarked-hq/falseok/blob/main/tests/pages) are rebuilt
from those cases with identifiers removed.

**Seen a block page falseok missed?** Send one entry in `signatures.json` and a small cleaned page in `tests/pages`.
That's the whole contribution: [docs/contributing.md](https://github.com/benchmarked-hq/falseok/blob/main/docs/contributing.md).

## Documentation

[Benchmark](https://github.com/benchmarked-hq/falseok/blob/main/docs/benchmark.md) · [How it works](https://github.com/benchmarked-hq/falseok/blob/main/docs/how-it-works.md) · [Verdicts](https://github.com/benchmarked-hq/falseok/blob/main/docs/verdicts.md) ·
[Data checks](https://github.com/benchmarked-hq/falseok/blob/main/docs/data-checks.md) · [Signatures](https://github.com/benchmarked-hq/falseok/blob/main/docs/signatures.md) · [Testing](https://github.com/benchmarked-hq/falseok/blob/main/docs/testing.md) ·
[Known limits](https://github.com/benchmarked-hq/falseok/blob/main/docs/limits.md) · [Contributing](https://github.com/benchmarked-hq/falseok/blob/main/docs/contributing.md)

## License

MIT. Made by [Benchmarked](https://benchmarked.website): we measure data tools against the most used alternatives on
price, speed and output.
