Skip to content

Python scraper based on AI

feat: align with scrapegraph-py v2 API surface from PR #82

Marco Vinciguerrac0f5fd592395
diff --git a/examples/markdownify/markdownify_scrapegraphai.py b/examples/markdownify/markdownify_scrapegraphai.py
index 66a1596c..30d9713f 100644
--- a/examples/markdownify/markdownify_scrapegraphai.py
+++ b/examples/markdownify/markdownify_scrapegraphai.py
@@ -1,33 +1,20 @@
"""
-Example script demonstrating the scrape functionality (v2 API - replaces markdownify)
+Scrape a webpage as clean markdown using scrapegraph-py v2 API.
+Replaces the old markdownify() call with scrape().
"""
import json
import os
+
from dotenv import load_dotenv
from scrapegraph_py import Client
-from scrapegraph_py.logger import sgai_logger
-
-def main():
- # Load environment variables
- load_dotenv()
- # Set up logging
- sgai_logger.set_logging(level="INFO")
+load_dotenv()
- # Initialize the client
- api_key = os.getenv("SCRAPEGRAPH_API_KEY")
- if not api_key:
- raise ValueError("SCRAPEGRAPH_API_KEY environment variable not found")
- sgai_client = Client(api_key=api_key)
+api_key = os.getenv("SCRAPEGRAPH_API_KEY")
+if not api_key:
+ raise ValueError("SCRAPEGRAPH_API_KEY environment variable not found")
- # Scrape a website as markdown (v2 API - replaces markdownify)
- print("Scraping website as Markdown")
- print("-" * 50)
- response = sgai_client.scrape(
- url="https://example.com"
- )
+with Client(api_key=api_key) as client:
+ response = client.scrape(url="https://example.com")
print(json.dumps(response, indent=2))
-
-if __name__ == "__main__":
- main()
diff --git a/examples/search_graph/scrapegraphai/searchscraper_scrapegraphai.py b/examples/search_graph/scrapegraphai/searchscraper_scrapegraphai.py
index 1f48adb6..2f8bce3f 100644
--- a/examples/search_graph/scrapegraphai/searchscraper_scrapegraphai.py
+++ b/examples/search_graph/scrapegraphai/searchscraper_scrapegraphai.py
@@ -1,43 +1,20 @@
"""
-Example implementation of search-based scraping using Scrapegraph AI v2 API.
-This example demonstrates how to use the search endpoint to extract information from the web.
+Search the web and extract AI-structured results using scrapegraph-py v2 API.
+Replaces the old searchscraper() call with search().
"""
import json
import os
+
from dotenv import load_dotenv
from scrapegraph_py import Client
-from scrapegraph_py.logger import sgai_logger
-
-def main():
- # Load environment variables
- load_dotenv()
-
- # Get API key
- api_key = os.getenv("SCRAPEGRAPH_API_KEY")
- if not api_key:
- raise ValueError("SCRAPEGRAPH_API_KEY not found in environment variables")
-
- # Configure logging
- sgai_logger.set_logging(level="INFO")
-
- # Initialize client
- sgai_client = Client(api_key=api_key)
-
- try:
- # Search request (v2 API - replaces searchscraper)
- print("\nSearching for information...")
- search_response = sgai_client.search(
- query="Extract webpage information"
- )
- print(json.dumps(search_response, indent=2))
+load_dotenv()
- except Exception as e:
- print(f"\nError occurred: {str(e)}")
- finally:
- # Always close the client
- sgai_client.close()
+api_key = os.getenv("SCRAPEGRAPH_API_KEY")
+if not api_key:
+ raise ValueError("SCRAPEGRAPH_API_KEY not found in environment variables")
-if __name__ == "__main__":
- main()
+with Client(api_key=api_key) as client:
+ response = client.search(query="Extract webpage information")
+ print(json.dumps(response, indent=2))
diff --git a/examples/smart_scraper_graph/scrapegraphai/smartscraper_scrapegraphai.py b/examples/smart_scraper_graph/scrapegraphai/smartscraper_scrapegraphai.py
index c78cf50f..944a6a6e 100644
--- a/examples/smart_scraper_graph/scrapegraphai/smartscraper_scrapegraphai.py
+++ b/examples/smart_scraper_graph/scrapegraphai/smartscraper_scrapegraphai.py
@@ -1,43 +1,23 @@
"""
-Example implementation using scrapegraph-py v2 client directly.
+Extract structured data from a webpage using scrapegraph-py v2 API.
+Replaces the old smartscraper() call with extract().
"""
import json
import os
+
from dotenv import load_dotenv
from scrapegraph_py import Client
-from scrapegraph_py.logger import sgai_logger
-
-def main():
- # Load environment variables from .env file
- load_dotenv()
-
- # Get API key from environment variables
- api_key = os.getenv("SCRAPEGRAPH_API_KEY")
- if not api_key:
- raise ValueError("SCRAPEGRAPH_API_KEY not found in environment variables")
-
- # Set up logging
- sgai_logger.set_logging(level="INFO")
-
- # Initialize the client with API key from environment
- sgai_client = Client(api_key=api_key)
-
- try:
- # Extract request (v2 API - replaces smartscraper)
- response = sgai_client.extract(
- url="https://scrapegraphai.com",
- prompt="Extract the founders' informations"
- )
- # Print the response
- print(json.dumps(response, indent=2))
+load_dotenv()
- except Exception as e:
- print(f"Error occurred: {str(e)}")
- finally:
- # Always close the client
- sgai_client.close()
+api_key = os.getenv("SCRAPEGRAPH_API_KEY")
+if not api_key:
+ raise ValueError("SCRAPEGRAPH_API_KEY not found in environment variables")
-if __name__ == "__main__":
- main()
+with Client(api_key=api_key) as client:
+ response = client.extract(
+ url="https://scrapegraphai.com",
+ prompt="Extract the founders' informations",
+ )
+ print(json.dumps(response, indent=2))
diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.py
index 6790d71b..2807125b 100644
--- a/scrapegraphai/graphs/smart_scraper_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_graph.py
@@ -79,30 +79,23 @@ class SmartScraperGraph(AbstractGraph):
if self.llm_model == "scrapegraphai/smart-scraper":
try:
from scrapegraph_py import Client
- from scrapegraph_py.logger import sgai_logger
except ImportError:
raise ImportError(
"scrapegraph_py is not installed. Please install it using 'pip install scrapegraph-py'."
)
- sgai_logger.set_logging(level="INFO")
-
- # Initialize the client with explicit API key
- sgai_client = Client(api_key=self.config.get("api_key"))
-
- # Extract request (v2 API)
- response = sgai_client.extract(
- url=self.source,
- prompt=self.prompt,
- )
-
- # Use logging instead of print for better production practices
- if "id" in response:
- logger.info(f"Request ID: {response['id']}")
- if "data" in response:
- logger.info(f"Result: {response['data']}")
+ with Client(api_key=self.config.get("api_key")) as sgai_client:
+ # Extract request (v2 API)
+ response = sgai_client.extract(
+ url=self.source,
+ prompt=self.prompt,
+ output_schema=self.schema,
+ )
- sgai_client.close()
+ if "id" in response:
+ logger.info(f"Request ID: {response['id']}")
+ if "data" in response:
+ logger.info(f"Result: {response['data']}")
return response