# Crawl a site

Visit the pages of a site in the background and find their problems.

A crawl runs in the background. You start it, check its status and read the results.

## Start

```bash
curl -X POST https://api.seoscraper.io/v1/crawls \
  -H "Authorization: Bearer ssk_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "max_pages": 500}'
```

The response has status `202` and the `id` of the crawl. All options are in [Start a crawl](/docs/api/start-crawl).

## Where addresses come from

| `source` | Behavior |
| --- | --- |
| `auto` | The sitemap, or links when there is no sitemap. This is the default. |
| `sitemap` | Only the sitemap. Without one the crawl fails with `sitemap_not_found`. |
| `links` | Only links, starting from the start page. |

The sitemap is taken from robots.txt, or from `/sitemap.xml`, `/sitemap_index.xml` and `/wp-sitemap.xml`. The `mode` field of the crawl tells which source was used.

A crawl stays on one site. `example.com` and `www.example.com` are the same site. Other subdomains need `include_subdomains`. Files such as images, PDFs, styles and scripts are not visited.

## Choose paths

`include_paths` and `exclude_paths` take up to 20 patterns each, written as in robots.txt. A pattern matches the beginning of the path, `*` is any text and `$` is the end:

```json
{
  "url": "https://example.com",
  "include_paths": ["/blog/"],
  "exclude_paths": ["/blog/tag/", "*?sort=", "*.html$"]
}
```

## Be gentle

- `concurrency` is the number of pages fetched at the same time, from 1 to 10.
- `delay_ms` is the pause between requests, up to 10 seconds.
- `respect_robots` is on by default. It obeys Disallow and Crawl-delay. Turn it off only for a site that is yours.

Our crawler introduces itself as `SeoScraperBot/1.0`. To block it on your site, add this to robots.txt:

```text
User-agent: SeoScraperBot
Disallow: /
```

## When the site answers 429

A server that gets too many requests answers `429`, or `503` with a `Retry-After` header. The crawl then:

1. pauses for as long as `Retry-After` asks, and without that header for 30 seconds and longer each time,
2. continues with one page at a time, with at least a second between requests,
3. fetches the rejected pages again, asking for each one at most 4 times.

Rejected attempts cost no tokens. The `throttle` field tells the number of pauses (`count`), the time between requests (`delay_ms`) and the end of the latest pause (`resume_at`).

The crawl ends with `stop_reason: throttled` when the server refuses 4 times in a row or asks for a pause longer than 15 minutes. Rejected pages stay in the results with status `429` and you can [retry them](#retry-pages).

## Check the status

Repeat every few seconds until `status` is no longer `queued` or `running`:

```python
import time
import requests

API = "https://api.seoscraper.io/v1/crawls"
HEADERS = {"Authorization": "Bearer ssk_YOUR_KEY"}

crawl = requests.post(API, headers=HEADERS, json={"url": "https://example.com"}, timeout=40).json()
while crawl["status"] in ("queued", "running"):
    time.sleep(3)
    crawl = requests.get(f"{API}/{crawl['id']}", headers=HEADERS, timeout=40).json()
    print(crawl["pages_done"], "of", crawl["pages_found"])
print(crawl["status"], crawl["stop_reason"])
```

| `status` | Meaning |
| --- | --- |
| `queued` | Waits for a free place |
| `running` | Pages are being fetched |
| `completed` | Finished, results are ready |
| `canceled` | Stopped by you, the collected pages are kept |
| `failed` | No results, the reason is in `error` |

| `stop_reason` | Why it ended |
| --- | --- |
| `finished` | There are no more addresses |
| `max_pages` | The page limit was reached |
| `no_tokens` | Fewer tokens were left than `max_pages` |
| `max_time` | The crawl ran for 2 hours |
| `throttled` | The server kept refusing requests despite pauses |
| `canceled` | You stopped it |
| `interrupted` | The connection was lost, the collected pages are kept |

## Read the results

Results are ready when the crawl ends. Before that you get `409 crawl_not_ready`.

- [`GET /v1/crawls/{id}/pages`](/docs/api/list-pages) lists pages with search, sorting and [filters](/docs/guides/filters).
- [`GET /v1/crawls/{id}/pages/{page_id}`](/docs/api/get-page) returns all data of one page.
- [`POST /v1/crawls/{id}/extract`](/docs/api/extract) [extracts data](/docs/guides/extract) with CSS selectors.
- [`GET /v1/crawls/{id}/download`](/docs/api/download) returns one [SQLite file](/docs/guides/sqlite) with everything.

## Problems

`summary.issues` has the number of pages with each problem. Use the same names in the `issue` parameter of the list of pages:

| `issue` | Meaning |
| --- | --- |
| `server_error` | Status 5xx |
| `client_error` | Status 404 and other 4xx |
| `too_many_requests` | Status 429 |
| `failed` | The page could not be fetched |
| `robots_blocked` | Skipped because of robots.txt |
| `redirected` | The address redirects |
| `slow` | Fetching took over 2 seconds |
| `missing_title` | No title |
| `long_title` | Title over 60 characters |
| `duplicate_title` | The same title on another page |
| `missing_description` | No meta description |
| `long_description` | Meta description over 155 characters |
| `duplicate_description` | The same meta description on another page |
| `missing_h1` | No h1 |
| `multiple_h1` | More than one h1 |
| `noindex` | The page may not be indexed |
| `other_canonical` | The canonical points to another address |
| `missing_alt` | Images without the alt attribute |

`ok` is also there: it counts pages with status 2xx.

## Retry pages

[`POST /v1/crawls/{id}/retry`](/docs/api/retry-pages) fetches the selected pages again and replaces them in the results of the same crawl. [`GET /v1/crawls/{id}/urls`](/docs/api/list-urls) returns the numbers of pages with a given problem:

```python
pages = requests.get(f"{API}/{crawl['id']}/urls", headers=HEADERS, params={"issue": "too_many_requests"}, timeout=40).json()["pages"]
ids = [page["id"] for page in pages]
retry = requests.post(f"{API}/{crawl['id']}/retry", headers=HEADERS, json={"page_ids": ids}, timeout=40).json()
```

The response is a separate crawl with its own `id`, progress and tokens. Check it the same way, and when it ends read the results of the original crawl. Only one retry can run at a time, otherwise you get `409 retry_in_progress`.

Pages with status 2xx stay unchanged when the new attempt fails. When a selected page has status 429, we fetch one page at a time from the start, at most one per second.

## Stop and delete

[`POST /v1/crawls/{id}/cancel`](/docs/api/cancel-crawl) stops a crawl and keeps the pages collected so far. [`DELETE /v1/crawls/{id}`](/docs/api/delete-crawl) removes the crawl with its results.
