Skip to content

Commit 45a6f0a

Browse files
Add runnable sync and async SDK examples
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 4822c33 commit 45a6f0a

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ python -m pytest -q
168168
python -m build
169169
```
170170

171+
## Examples
172+
173+
Ready-to-run examples are available in `examples/`:
174+
175+
- `examples/sync_scrape.py`
176+
- `examples/async_extract.py`
177+
171178
## License
172179

173180
MIT — see [LICENSE](LICENSE).

examples/async_extract.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Asynchronous extract example.
3+
4+
Run:
5+
export HYPERBROWSER_API_KEY="your_api_key"
6+
python examples/async_extract.py
7+
"""
8+
9+
import asyncio
10+
11+
from hyperbrowser import AsyncHyperbrowser
12+
from hyperbrowser.models import StartExtractJobParams
13+
14+
15+
async def main() -> None:
16+
async with AsyncHyperbrowser() as client:
17+
result = await client.extract.start_and_wait(
18+
StartExtractJobParams(
19+
urls=["https://hyperbrowser.ai"],
20+
prompt="Extract the main product value propositions as a list.",
21+
),
22+
poll_interval_seconds=1.0,
23+
max_wait_seconds=120.0,
24+
)
25+
26+
print(result.status)
27+
print(result.data)
28+
29+
30+
if __name__ == "__main__":
31+
asyncio.run(main())

examples/sync_scrape.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Synchronous scrape example.
3+
4+
Run:
5+
export HYPERBROWSER_API_KEY="your_api_key"
6+
python examples/sync_scrape.py
7+
"""
8+
9+
from hyperbrowser import Hyperbrowser
10+
from hyperbrowser.exceptions import HyperbrowserTimeoutError
11+
from hyperbrowser.models import ScrapeOptions, StartScrapeJobParams
12+
13+
14+
def main() -> None:
15+
with Hyperbrowser() as client:
16+
try:
17+
result = client.scrape.start_and_wait(
18+
StartScrapeJobParams(
19+
url="https://hyperbrowser.ai",
20+
scrape_options=ScrapeOptions(formats=["markdown"]),
21+
),
22+
poll_interval_seconds=1.0,
23+
max_wait_seconds=120.0,
24+
)
25+
except HyperbrowserTimeoutError:
26+
print("Scrape job timed out.")
27+
return
28+
29+
if result.data and result.data.markdown:
30+
print(result.data.markdown[:500])
31+
else:
32+
print(f"Scrape finished with status={result.status} and no markdown payload.")
33+
34+
35+
if __name__ == "__main__":
36+
main()

0 commit comments

Comments
 (0)