# SQLite file

The whole crawl in one file that opens in any SQLite program.

## Download

In the panel click **Download SQLite** on the page of a crawl. In the API:

```bash
curl -L --compressed -o crawl.sqlite \
  https://api.seoscraper.io/v1/crawls/CRAWL_ID/download \
  -H "Authorization: Bearer ssk_YOUR_KEY"
```

The file is sent compressed with gzip. Browsers and most HTTP libraries unpack it on their own, and curl needs `--compressed`.

Open it with the `sqlite3` program, [DB Browser for SQLite](https://sqlitebrowser.org), Python, DuckDB or any other tool that reads SQLite.

## Tables

### crawl

One row about the whole crawl: `start_url`, `options` (JSON), `mode`, `stop_reason`, `started_at`, `finished_at`, `pages`.

### pages

| Column | Content |
| --- | --- |
| `id` | Page number, the same as in the API |
| `url`, `final_url`, `path` | Address, address after redirects, path |
| `status`, `error` | HTTP status, or an error code when there was no response |
| `depth`, `found_on` | Clicks from the start page, page with the first link |
| `content_type`, `lang` | Content type, language |
| `title`, `meta_description`, `canonical` | Metadata |
| `robots_meta`, `x_robots_tag` | Meta robots, X-Robots-Tag header |
| `is_index`, `is_follow` | 1 when indexing and following links is allowed |
| `word_count` | Number of words |
| `duration_ms`, `size_bytes`, `truncated` | Fetch time, size, 1 when the content was cut |
| `redirects` | Redirects as JSON |

### Other tables

Each has `page_id`, which points to `pages.id`.

| Table | Columns |
| --- | --- |
| `links` | `url`, `text`, `type` (internal, external, other), `rel`, `nofollow` |
| `images` | `src`, `alt`, `lazy`, `width`, `height` |
| `headings` | `level`, `text` |
| `headers` | `name`, `value` |
| `html` | `html`, compressed with zstd |

## Sample queries

Broken links and the pages they are on:

```sql
SELECT source.path, links.text, target.url, target.status
FROM links
JOIN pages AS source ON source.id = links.page_id
JOIN pages AS target ON target.url = links.url
WHERE target.status >= 400
ORDER BY target.url;
```

Pages with the same title:

```sql
SELECT title, count(*) AS pages, group_concat(path, ', ') AS paths
FROM pages
WHERE title <> ''
GROUP BY title
HAVING count(*) > 1
ORDER BY pages DESC;
```

Pages with the fewest internal links leading to them:

```sql
SELECT pages.url, count(links.url) AS incoming
FROM pages
LEFT JOIN links ON links.url = pages.url AND links.type = 'internal' AND links.page_id <> pages.id
GROUP BY pages.id
ORDER BY incoming;
```

Images without alt:

```sql
SELECT pages.url, images.src
FROM images
JOIN pages ON pages.id = images.page_id
WHERE images.alt IS NULL;
```

## Raw HTML

HTML is compressed with zstd. In Python, after `pip install zstandard`:

```python
import sqlite3
import zstandard

db = sqlite3.connect("crawl.sqlite")
unpack = zstandard.ZstdDecompressor().decompress
for url, html in db.execute("SELECT url, html FROM html JOIN pages ON pages.id = html.page_id"):
    print(url, len(unpack(html).decode()))
```
