> ## Documentation Index
> Fetch the complete documentation index at: /llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Screenshot, search, scrape and crawl the web with the ScreenshotRender Python SDK, and get clean Markdown or structured data.

## Installation

To install the ScreenshotRender Python SDK, use pip:

```bash Python
pip install screenshotrender
```

It needs Python 3.9 or later.

## Usage

1. Get an API key from your [dashboard](https://www.screenshotrender.com/dashboard). New accounts start with 100 free credits.
2. Pass it to the client as `api_key`, or set it in the `SCREENSHOTRENDER_API_KEY` environment variable.

Each method returns the response's `data`. Results are dicts whose fields also read as attributes, under the API's name or in snake\_case: `doc.metadata.source_url` is `doc["metadata"]["sourceURL"]`.

### Taking a screenshot

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

shot = screenshotrender.screenshot("https://example.com")
print(shot.screenshot)  # the image's URL
```

Capture the full page, at a window size you choose:

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

shot = screenshotrender.screenshot(
    "https://www.screenshotrender.com",
    full_page=True,
    viewport={"width": 1440, "height": 900},
)
print(shot.screenshot)
```

### Searching the web

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

results = screenshotrender.search(
    "open source vector databases",
    limit=2,
    scrape_options={"formats": ["markdown"]},
)

for result in results.web or []:
    print(result.url, len(result.markdown or ""), "characters")
```

### Scraping a URL

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

doc = screenshotrender.scrape("https://example.com", formats=["markdown", "html"])
print(doc.markdown)
```

### Extracting structured data

The schema can be a Pydantic model:

```python Python
from pydantic import BaseModel
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

class Story(BaseModel):
    title: str
    points: int

class TopStories(BaseModel):
    stories: list[Story]

doc = screenshotrender.scrape(
    "https://news.ycombinator.com",
    formats=[{"type": "json", "prompt": "The top 3 stories", "schema": TopStories}],
)
print(doc.json)
```

### Parsing documents

From a URL:

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

paper = screenshotrender.parse(
    "https://arxiv.org/pdf/1706.03762",
    parsers=[{"type": "pdf", "max_pages": 2}],
)
print(paper.markdown)
```

From a file: pass a path, bytes or an open file.

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

report = screenshotrender.parse("report.pdf")  # a path, bytes or an open file
print(report.markdown)
```

### Mapping a website

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

site = screenshotrender.map("https://docs.python.org/3/", limit=5)

for link in site.links:
    print(link.url, link.title)
```

### Crawling a website

`crawl()` starts the crawl, waits for it and returns every page. `on_progress` is called after every status check.

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

crawl = screenshotrender.crawl(
    "https://docs.python.org/3/tutorial/",
    limit=3,
    on_progress=lambda job: print(f"{job.completed} of {job.total} pages"),
)

print(crawl.status, crawl.credits_used, "credits")
for page in crawl.data:
    print(page.metadata.source_url)
```

### Start a crawl

To start a crawl without waiting, use `start_crawl()` and keep the `id`:

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

job = screenshotrender.start_crawl("https://docs.python.org/3/tutorial/", limit=3)
print(job.id)
```

### Checking crawl status

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

job = screenshotrender.get_crawl_status("YOUR_CRAWL_ID")
print(job.status, f"{job.completed} of {job.total} pages")
```

### Cancelling a crawl

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

stopped = screenshotrender.cancel_crawl("YOUR_CRAWL_ID")
print(stopped)  # False when the crawl had already finished
```

### Failed and blocked pages

```python Python
from screenshotrender import ScreenshotRender

screenshotrender = ScreenshotRender(api_key="sr-YOUR-API-KEY")

report = screenshotrender.get_crawl_errors("YOUR_CRAWL_ID")
print(report.errors, report.robots_blocked)
```

## Error handling

Every failure is a `ScreenshotRenderError` with the HTTP `status` and the API's error `code`. See [Errors](/api-reference/errors.md) for every code.

```python Python
from screenshotrender import ScreenshotRender, ScreenshotRenderError

screenshotrender = ScreenshotRender(api_key="sr-not-a-real-key")

try:
    screenshotrender.scrape("https://example.com")
except ScreenshotRenderError as err:
    print(err.status, err.code, err)  # 401 invalid_api_key Invalid API key.
```

## Async Class

`AsyncScreenshotRender` has the same methods, as coroutines. Use it to run requests at the same time:

```python Python
import asyncio
from screenshotrender import AsyncScreenshotRender

async def main():
    # Reads your key from SCREENSHOTRENDER_API_KEY
    async with AsyncScreenshotRender() as screenshotrender:
        pages = await asyncio.gather(
            screenshotrender.scrape("https://example.com"),
            screenshotrender.scrape("https://www.iana.org/help/example-domains"),
        )
        for page in pages:
            print(page.metadata.title)

asyncio.run(main())
```
