You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5c53e24
Browse filesBrowse the repository at this point in the historyBrowse files
The Context Dev Python library provides convenient access to the Context Dev REST API from any Python 3.9+
7
-
application. The library includes type definitions for all request params and response fields,
8
-
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
9
-
10
-
It is generated with [Stainless](https://www.stainless.com/).
6
+
Context.dev is a web scraping API for AI agents and LLMs. This SDK turns any URL into clean, LLM-ready markdown, crawls whole sites, searches the web, takes screenshots and extracts structured JSON against a schema you define, all with one API key. Proxies, JavaScript rendering and anti-bot handling run on Context.dev's side, so there is no headless browser to host.
11
7
12
8
## Documentation
13
9
@@ -22,23 +18,101 @@ pip install context.dev
22
18
23
19
## Usage
24
20
21
+
Set `CONTEXT_DEV_API_KEY` to your API key; the client reads it automatically.
22
+
23
+
### Scrape markdown and HTML
24
+
25
25
The full API of this library can be found in [api.md](api.md).
26
26
27
27
```python
28
-
import os
29
28
from context.dev import ContextDev
30
29
31
-
client = ContextDev(
32
-
api_key=os.environ.get("CONTEXT_DEV_API_KEY"), # This is the default and can be omitted
30
+
client = ContextDev()
31
+
32
+
page = client.web.scrape(
33
+
url="https://example.com",
34
+
formats={"markdown": True, "html": True},
35
+
)
36
+
print(page.markdown.data)
37
+
print(page.html.data)
38
+
```
39
+
40
+
### Extract structured JSON
41
+
42
+
```python
43
+
from context.dev import ContextDev
44
+
45
+
client = ContextDev()
46
+
47
+
page = client.web.scrape(
48
+
url="https://example.com",
49
+
formats={"json": True},
50
+
json_params={
51
+
"schema": {
52
+
"type": "object",
53
+
"properties": {
54
+
"title": {"type": ["string", "null"]},
55
+
"description": {"type": ["string", "null"]},
56
+
},
57
+
"required": ["title", "description"],
58
+
"additionalProperties": False,
59
+
},
60
+
},
61
+
)
62
+
print(page.json_.data)
63
+
```
64
+
65
+
### Extract relevant highlights
66
+
67
+
Return the passages that answer a question about the page.
68
+
69
+
```python
70
+
from context.dev import ContextDev
71
+
72
+
client = ContextDev()
73
+
74
+
page = client.web.scrape(
75
+
url="https://example.com",
76
+
formats={"highlights": True},
77
+
highlights_params={"query": "What is this domain used for?"},
33
78
)
79
+
print(page.highlights.data)
80
+
```
81
+
82
+
### Take a screenshot
83
+
84
+
The screenshot is returned as a base64 image data URL.
34
85
35
-
brand = client.brand.retrieve(
36
-
domain="REPLACE_ME",
37
-
type="by_domain",
86
+
```python
87
+
from context.dev import ContextDev
88
+
89
+
client = ContextDev()
90
+
91
+
page = client.web.scrape(
92
+
url="https://example.com",
93
+
formats={"screenshot": True},
38
94
)
39
-
print(brand.request_id)
95
+
print(page.screenshot.data)
40
96
```
41
97
98
+
## What you can do
99
+
100
+
| Task | Method |
101
+
| --- | --- |
102
+
| Scrape a URL to markdown, HTML, JSON, highlights or a screenshot |`client.web.scrape`|
103
+
| Crawl a site and get every page as markdown |`client.web.web_crawl_md`|
104
+
| Map every URL on a domain |`client.web.map_urls`|
105
+
| Search the web |`client.web.search`|
106
+
| Take a screenshot of a page |`client.web.screenshot`|
107
+
| Parse PDFs and documents |`client.parse.handle`|
108
+
| Run thousands of URLs as a batch |`client.batch.submit`|
109
+
| Watch a page for changes |`client.monitors.create`|
110
+
| Look up a company's logo, colors and brand data |`client.brand.retrieve`|
111
+
112
+
## Use it from an AI agent
113
+
114
+
Context.dev also ships as a plugin for [Claude](https://github.com/context-dot-dev/claude-plugin), [Cursor](https://github.com/context-dot-dev/cursor-plugin) and [Gemini CLI](https://github.com/context-dot-dev/gemini-cli-context), and as tools for [LangChain](https://github.com/context-dot-dev/langchain-context) and [Haystack](https://github.com/context-dot-dev/context-haystack).
115
+
42
116
While you can provide an `api_key` keyword argument,
43
117
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
44
118
to add `CONTEXT_DEV_API_KEY="My API Key"` to your `.env` file
@@ -59,11 +133,11 @@ client = AsyncContextDev(
59
133
60
134
61
135
asyncdefmain() -> None:
62
-
brand=await client.brand.retrieve(
63
-
domain="REPLACE_ME",
64
-
type="by_domain",
136
+
page=await client.web.scrape(
137
+
url="https://example.com",
138
+
formats={"markdown": True},
65
139
)
66
-
print(brand.request_id)
140
+
print(page.markdown.data)
67
141
68
142
69
143
asyncio.run(main())
@@ -96,11 +170,11 @@ async def main() -> None:
96
170
api_key=os.environ.get("CONTEXT_DEV_API_KEY"), # This is the default and can be omitted
97
171
http_client=DefaultAioHttpClient(),
98
172
) as client:
99
-
brand=await client.brand.retrieve(
100
-
domain="REPLACE_ME",
101
-
type="by_domain",
173
+
page=await client.web.scrape(
174
+
url="https://example.com",
175
+
formats={"markdown": True},
102
176
)
103
-
print(brand.request_id)
177
+
print(page.markdown.data)
104
178
105
179
106
180
asyncio.run(main())
@@ -124,12 +198,12 @@ from context.dev import ContextDev
124
198
125
199
client = ContextDev()
126
200
127
-
brand= client.brand.retrieve(
128
-
domain="xxx",
129
-
type="by_domain",
201
+
page= client.web.scrape(
202
+
url="https://example.com",
203
+
formats={"markdown": True},
130
204
timeout_opts={"milliseconds": 1000},
131
205
)
132
-
print(brand.timeout_opts)
206
+
print(page.markdown.data)
133
207
```
134
208
135
209
## Handling errors
@@ -148,9 +222,9 @@ from context.dev import ContextDev
brand= response.parse()# get the object that `brand.retrieve()` would have returned
274
-
print(brand.request_id)
348
+
page= response.parse()
349
+
print(page.markdown.data)
275
350
```
276
351
277
352
These methods return an [`APIResponse`](https://github.com/context-dot-dev/context-python-sdk/tree/main/src/context/dev/_response.py) object.
@@ -285,9 +360,9 @@ The above interface eagerly reads the full response body when you make the reque
285
360
To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.
286
361
287
362
```python
288
-
with client.brand.with_streaming_response.retrieve(
0 commit comments