Skip to content

Commit 2881428

Browse files
authored
jupyter notebook example (#129)
1 parent f815d3f commit 2881428

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"%pip install stagehand"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 75,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import os\n",
19+
"from pprint import pprint"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"import dotenv\n",
29+
"dotenv.load_dotenv()\n"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 77,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"from stagehand import Stagehand, StagehandConfig\n",
39+
"\n",
40+
"stagehand = Stagehand(StagehandConfig(\n",
41+
" env=\"BROWSERBASE\",\n",
42+
" model_name=\"openai/gpt-4o-mini\",\n",
43+
" api_key=os.getenv(\"BROWSERBASE_API_KEY\"),\n",
44+
" project_id=os.getenv(\"BROWSERBASE_PROJECT_ID\"),\n",
45+
" api_url=\"https://api.stagehand.browserbase.com/v1\",\n",
46+
" # headless=False,\n",
47+
" verbose=0,\n",
48+
" # dom_settle_timeout_ms=3000,\n",
49+
"), model_api_key=os.getenv(\"OPENAI_API_KEY\"))"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 78,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"await stagehand.init()"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 79,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"page = stagehand.page\n",
68+
"await page.goto(\"https://www.hackernews.com\")"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 82,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"from typing import List\n",
78+
"\n",
79+
"from pydantic import BaseModel\n",
80+
"\n",
81+
"\n",
82+
"class Post(BaseModel):\n",
83+
" title: str\n",
84+
" points: int\n",
85+
" comments: int\n",
86+
"\n",
87+
"class Posts(BaseModel):\n",
88+
" posts: List[Post]\n",
89+
"\n",
90+
"res = await page.extract(\"find all the posts related to the topic 'AI'\", schema=Posts)"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"pprint(res.posts)"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 85,
105+
"metadata": {},
106+
"outputs": [],
107+
"source": [
108+
"await stagehand.close()"
109+
]
110+
}
111+
],
112+
"metadata": {
113+
"kernelspec": {
114+
"display_name": "venv",
115+
"language": "python",
116+
"name": "python3"
117+
},
118+
"language_info": {
119+
"codemirror_mode": {
120+
"name": "ipython",
121+
"version": 3
122+
},
123+
"file_extension": ".py",
124+
"mimetype": "text/x-python",
125+
"name": "python",
126+
"nbconvert_exporter": "python",
127+
"pygments_lexer": "ipython3",
128+
"version": "3.11.12"
129+
}
130+
},
131+
"nbformat": 4,
132+
"nbformat_minor": 2
133+
}

0 commit comments

Comments
 (0)