Skip to content

Commit 2e477d1

Browse files
committed
feat: update examples
1 parent 16426de commit 2e477d1

6 files changed

+107
-20
lines changed

scrapegraph-py/examples/async/async_smartscraper_example.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import asyncio
2+
import os
3+
from dotenv import load_dotenv
24

35
from scrapegraph_py import AsyncClient
46
from scrapegraph_py.logger import sgai_logger
57

8+
# Load environment variables from .env file
9+
load_dotenv()
10+
611
sgai_logger.set_logging(level="INFO")
712

813

914
async def main():
10-
11-
# Initialize async client
12-
sgai_client = AsyncClient(api_key="your-api-key-here")
15+
# Initialize async client with API key from environment variable
16+
api_key = os.getenv("SGAI_API_KEY")
17+
if not api_key:
18+
print("❌ Error: SGAI_API_KEY environment variable not set")
19+
print("Please either:")
20+
print(" 1. Set environment variable: export SGAI_API_KEY='your-api-key-here'")
21+
print(" 2. Create a .env file with: SGAI_API_KEY=your-api-key-here")
22+
return
23+
24+
sgai_client = AsyncClient(api_key=api_key)
1325

1426
# Concurrent scraping requests
1527
urls = [

scrapegraph-py/examples/async/async_smartscraper_pagination_example.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
import time
1313
from pydantic import BaseModel
1414
from typing import List, Optional
15+
from dotenv import load_dotenv
1516

1617
from scrapegraph_py import AsyncClient
1718
from scrapegraph_py.exceptions import APIError
1819

20+
# Load environment variables from .env file
21+
load_dotenv()
22+
1923

2024
# Configure logging
2125
logging.basicConfig(
@@ -47,11 +51,18 @@ async def smartscraper_pagination_example():
4751
print("=" * 50)
4852

4953
# Initialize client from environment variable
54+
api_key = os.getenv("SGAI_API_KEY")
55+
if not api_key:
56+
print("❌ Error: SGAI_API_KEY environment variable not set")
57+
print("Please either:")
58+
print(" 1. Set environment variable: export SGAI_API_KEY='your-api-key-here'")
59+
print(" 2. Create a .env file with: SGAI_API_KEY=your-api-key-here")
60+
return
61+
5062
try:
51-
client = AsyncClient.from_env()
52-
except ValueError as e:
63+
client = AsyncClient(api_key=api_key)
64+
except Exception as e:
5365
print(f"❌ Error initializing client: {e}")
54-
print("Please set SGAI_API_KEY environment variable")
5566
return
5667

5768
# Configuration
@@ -122,9 +133,14 @@ async def test_concurrent_pagination():
122133
print("Testing concurrent pagination requests")
123134
print("=" * 50)
124135

136+
api_key = os.getenv("SGAI_API_KEY")
137+
if not api_key:
138+
print("❌ Error: SGAI_API_KEY environment variable not set")
139+
return
140+
125141
try:
126-
client = AsyncClient.from_env()
127-
except ValueError as e:
142+
client = AsyncClient(api_key=api_key)
143+
except Exception as e:
128144
print(f"❌ Error initializing client: {e}")
129145
return
130146

@@ -182,9 +198,14 @@ async def test_pagination_with_different_parameters():
182198
print("Testing pagination with different parameters")
183199
print("=" * 50)
184200

201+
api_key = os.getenv("SGAI_API_KEY")
202+
if not api_key:
203+
print("❌ Error: SGAI_API_KEY environment variable not set")
204+
return
205+
185206
try:
186-
client = AsyncClient.from_env()
187-
except ValueError as e:
207+
client = AsyncClient(api_key=api_key)
208+
except Exception as e:
188209
print(f"❌ Error initializing client: {e}")
189210
return
190211

scrapegraph-py/examples/sync/smartscraper_example.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
import os
2+
from dotenv import load_dotenv
13
from scrapegraph_py import Client
24
from scrapegraph_py.logger import sgai_logger
35

6+
# Load environment variables from .env file
7+
load_dotenv()
8+
49
sgai_logger.set_logging(level="INFO")
510

6-
# Initialize the client with explicit API key
7-
sgai_client = Client(api_key="your-api-key-here")
11+
# Initialize the client with API key from environment variable
12+
api_key = os.getenv("SGAI_API_KEY")
13+
if not api_key:
14+
print("❌ Error: SGAI_API_KEY environment variable not set")
15+
print("Please either:")
16+
print(" 1. Set environment variable: export SGAI_API_KEY='your-api-key-here'")
17+
print(" 2. Create a .env file with: SGAI_API_KEY=your-api-key-here")
18+
exit(1)
19+
20+
sgai_client = Client(api_key=api_key)
821

922
# SmartScraper request
1023
response = sgai_client.smartscraper(

scrapegraph-py/examples/sync/smartscraper_infinite_scroll_example.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import os
2+
from dotenv import load_dotenv
23
from scrapegraph_py import Client
34
from scrapegraph_py.logger import sgai_logger
45
from pydantic import BaseModel
56
from typing import List
67

8+
# Load environment variables from .env file
9+
load_dotenv()
10+
711
sgai_logger.set_logging(level="INFO")
812

913
# Define the output schema
@@ -17,7 +21,15 @@ class CompaniesResponse(BaseModel):
1721

1822
# Initialize the client with API key from environment variable
1923
# Make sure to set SGAI_API_KEY in your environment or .env file
20-
sgai_client = Client.from_env()
24+
api_key = os.getenv("SGAI_API_KEY")
25+
if not api_key:
26+
print("❌ Error: SGAI_API_KEY environment variable not set")
27+
print("Please either:")
28+
print(" 1. Set environment variable: export SGAI_API_KEY='your-api-key-here'")
29+
print(" 2. Create a .env file with: SGAI_API_KEY=your-api-key-here")
30+
exit(1)
31+
32+
sgai_client = Client(api_key=api_key)
2133

2234
try:
2335
# SmartScraper request with infinite scroll

scrapegraph-py/examples/sync/smartscraper_pagination_example.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
import time
1212
from pydantic import BaseModel
1313
from typing import List, Optional
14+
from dotenv import load_dotenv
1415

1516
from scrapegraph_py import Client
1617
from scrapegraph_py.exceptions import APIError
1718

19+
# Load environment variables from .env file
20+
load_dotenv()
21+
1822

1923
# Configure logging
2024
logging.basicConfig(
@@ -46,11 +50,18 @@ def smartscraper_pagination_example():
4650
print("=" * 50)
4751

4852
# Initialize client from environment variable
53+
api_key = os.getenv("SGAI_API_KEY")
54+
if not api_key:
55+
print("❌ Error: SGAI_API_KEY environment variable not set")
56+
print("Please either:")
57+
print(" 1. Set environment variable: export SGAI_API_KEY='your-api-key-here'")
58+
print(" 2. Create a .env file with: SGAI_API_KEY=your-api-key-here")
59+
return
60+
4961
try:
50-
client = Client.from_env()
51-
except ValueError as e:
62+
client = Client(api_key=api_key)
63+
except Exception as e:
5264
print(f"❌ Error initializing client: {e}")
53-
print("Please set SGAI_API_KEY environment variable")
5465
return
5566

5667
# Configuration
@@ -121,9 +132,14 @@ def test_pagination_parameters():
121132
print("Testing different pagination parameters")
122133
print("=" * 50)
123134

135+
api_key = os.getenv("SGAI_API_KEY")
136+
if not api_key:
137+
print("❌ Error: SGAI_API_KEY environment variable not set")
138+
return
139+
124140
try:
125-
client = Client.from_env()
126-
except ValueError as e:
141+
client = Client(api_key=api_key)
142+
except Exception as e:
127143
print(f"❌ Error initializing client: {e}")
128144
return
129145

scrapegraph-py/examples/sync/smartscraper_schema_example.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import os
2+
from dotenv import load_dotenv
13
from pydantic import BaseModel, Field
24

35
from scrapegraph_py import Client
46

7+
# Load environment variables from .env file
8+
load_dotenv()
9+
510

611
# Define a Pydantic model for the output schema
712
class WebpageSchema(BaseModel):
@@ -10,8 +15,16 @@ class WebpageSchema(BaseModel):
1015
summary: str = Field(description="A brief summary of the webpage")
1116

1217

13-
# Initialize the client
14-
sgai_client = Client(api_key="your-api-key-here")
18+
# Initialize the client with API key from environment variable
19+
api_key = os.getenv("SGAI_API_KEY")
20+
if not api_key:
21+
print("❌ Error: SGAI_API_KEY environment variable not set")
22+
print("Please either:")
23+
print(" 1. Set environment variable: export SGAI_API_KEY='your-api-key-here'")
24+
print(" 2. Create a .env file with: SGAI_API_KEY=your-api-key-here")
25+
exit(1)
26+
27+
sgai_client = Client(api_key=api_key)
1528

1629
# SmartScraper request with output schema
1730
response = sgai_client.smartscraper(

0 commit comments

Comments
 (0)