mirror of
https://github.com/VinciGit00/Scrapegraph-ai.git
synced 2026-07-09 21:19:20 +08:00
55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
"""
|
|
Basic example of scraping pipeline using SmartScraper with schema
|
|
"""
|
|
import json
|
|
import os
|
|
from typing import Dict
|
|
from dotenv import load_dotenv
|
|
from pydantic import BaseModel
|
|
from scrapegraphai.graphs import SmartScraperGraph
|
|
|
|
load_dotenv()
|
|
|
|
# ************************************************
|
|
# Define the output schema for the graph
|
|
# ************************************************
|
|
class Project(BaseModel):
|
|
title: str
|
|
description: str
|
|
|
|
|
|
class Projects(BaseModel):
|
|
Projects: Dict[str, Project]
|
|
|
|
|
|
# ************************************************
|
|
# Define the configuration for the graph
|
|
# ************************************************
|
|
|
|
openai_key = os.getenv("OPENAI_APIKEY")
|
|
|
|
graph_config = {
|
|
"llm": {
|
|
"model": "ernie/ernie-bot-turbo",
|
|
"ernie_client_id": "<ernie_client_id>",
|
|
"ernie_client_secret": "<ernie_client_secret>",
|
|
"temperature": 0.1
|
|
},
|
|
"verbose": True,
|
|
"headless": False,
|
|
}
|
|
|
|
# ************************************************
|
|
# Create the SmartScraperGraph instance and run it
|
|
# ************************************************
|
|
|
|
smart_scraper_graph = SmartScraperGraph(
|
|
prompt="List me all the projects with their description",
|
|
source="https://perinim.github.io/projects/",
|
|
schema=Projects,
|
|
config=graph_config,
|
|
)
|
|
|
|
result = smart_scraper_graph.run()
|
|
print(json.dumps(result, indent=4))
|