-
Notifications
You must be signed in to change notification settings - Fork 0
/
1b-smart_scraper_schema.py
51 lines (41 loc) · 1.31 KB
/
1b-smart_scraper_schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Basic example of scraping pipeline using SmartScraper with schema
"""
import os
import json
from typing import List
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from scrapegraphai.graphs import SmartScraperGraph
load_dotenv()
# ************************************************
# Define the output schema for the graph
# ************************************************
class Article(BaseModel):
title: str = Field(description="The title of the article")
author: str = Field(description="The author of the article")
class Articles(BaseModel):
article: List[Article]
# ************************************************
# Define the configuration for the graph
# ************************************************
openai_key = os.getenv("OPENAI_APIKEY")
graph_config = {
"llm": {
"api_key": openai_key,
"model": "openai/gpt-4o-mini",
},
"verbose": True,
"headless": False,
}
# ************************************************
# Create the SmartScraperGraph instance and run it
# ************************************************
smart_scraper_graph = SmartScraperGraph(
source="https://www.wired.com",
prompt="Extract me all the articles",
schema=Articles,
config=graph_config
)
result = smart_scraper_graph.run()
print(json.dumps(result, indent=4))