Skip to content

Python scraper based on AI

add examples + module documentation

VinciGit00906fea92e95d
diff --git a/examples/direct use/Readme.md b/examples/direct use/Readme.md
new file mode 100644
index 00000000..7b8e226b
--- /dev/null
+++ b/examples/direct use/Readme.md
@@ -0,0 +1,2 @@
+For having interacting examples visit the following collab notebook:
+[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1sEZBonBMGP44CtO6GQTwAlL0BGJXjtfd?usp=sharing)
diff --git a/examples/direct use/custom_graph.py b/examples/direct use/custom_graph.py
new file mode 100644
index 00000000..aa9445d7
--- /dev/null
+++ b/examples/direct use/custom_graph.py
@@ -0,0 +1,54 @@
+
+"""
+Example of custom graph using existing nodes
+"""
+from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode
+from scrapegraphai.graphs import BaseGraph
+from scrapegraphai.models import OpenAI
+from scrapegraphai.helpers import nodes_metadata
+
+OPENAI_API_KEY = "YOUR_API_KEY"
+
+# check available nodes
+
+nodes_metadata.keys()
+
+# to get more information about a node
+print(nodes_metadata['ImageToTextNode'])
+
+# Define the configuration for the language model
+llm_config = {
+ "api_key": OPENAI_API_KEY,
+ "model_name": "gpt-3.5-turbo",
+ "temperature": 0,
+ "streaming": True
+}
+model = OpenAI(llm_config)
+
+# define the nodes for the graph
+fetch_html_node = FetchHTMLNode("fetch_html")
+parse_document_node = ParseHTMLNode("parse_document")
+generate_answer_node = GenerateAnswerNode(model, "generate_answer")
+
+# create the graph
+graph = BaseGraph(
+ nodes={
+ fetch_html_node,
+ parse_document_node,
+ generate_answer_node
+ },
+ edges={
+ (fetch_html_node, parse_document_node),
+ (parse_document_node, generate_answer_node)
+ },
+ entry_point=fetch_html_node
+)
+
+# execute the graph
+inputs = {"user_input": "What is the title of the page?",
+ "url": "https://example.com"}
+result = graph.execute(inputs)
+
+# get the answer from the result
+answer = result.get("answer", "No answer found.")
+print(answer)
diff --git a/examples/direct use/graph_builder.py b/examples/direct use/graph_builder.py
new file mode 100644
index 00000000..87cf21ee
--- /dev/null
+++ b/examples/direct use/graph_builder.py
@@ -0,0 +1,23 @@
+"""
+Example of graph building
+"""
+from scrapegraphai.builders import GraphBuilder
+OPENAI_API_KEY = "YOUR_API_KEY"
+
+
+llm_config = {
+ "api_key": OPENAI_API_KEY,
+ "model_name": "gpt-3.5-turbo",
+ "temperature": 0,
+ "streaming": True
+}
+
+# Example usage of GraphBuilder
+USER_PROMPT = "Extract the news and generate a text summary with a voiceover."
+graph_builder = GraphBuilder(USER_PROMPT, llm_config)
+graph_json = graph_builder.build_graph()
+
+# Convert the resulting JSON to Graphviz format
+graphviz_graph = graph_builder.convert_json_to_graphviz(graph_json)
+
+print(graph_json)
diff --git a/examples/direct use/smart_scraper_graph.py b/examples/direct use/smart_scraper_graph.py
new file mode 100644
index 00000000..6eacc6b1
--- /dev/null
+++ b/examples/direct use/smart_scraper_graph.py
@@ -0,0 +1,17 @@
+"""
+Basic example of scraping pipeline using SmartScraper
+"""
+from scrapegraphai.graphs import SmartScraperGraph
+OPENAI_API_KEY = "YOUR_API_KEY"
+
+
+llm_config = {
+ "api_key": OPENAI_API_KEY,
+ "model_name": "gpt-3.5-turbo",
+}
+
+smart_scraper_graph = SmartScraperGraph("List me all the titles and project descriptions",
+ "https://perinim.github.io/projects/", llm_config)
+
+answer = smart_scraper_graph.run()
+print(answer["projects"][0])
diff --git a/examples/direct use/speech_summary_graph.py b/examples/direct use/speech_summary_graph.py
new file mode 100644
index 00000000..859bcdc8
--- /dev/null
+++ b/examples/direct use/speech_summary_graph.py
@@ -0,0 +1,20 @@
+"""
+Basic example of scraping pipeline using SpeechSummaryGraph
+"""
+
+from scrapegraphai.graphs import SpeechSummaryGraph
+OPENAI_API_KEY = "YOUR_API_KEY"
+
+
+llm_config = {
+ "api_key": OPENAI_API_KEY
+}
+
+# Save the audio to a file
+AUDIO_FILE = "website_summary.mp3"
+SPEECH_SUMMARY_GRAPH = SpeechSummaryGraph("Make a summary of the webpage to be converted to audio for blind people.",
+ "https://perinim.github.io/projects/", llm_config,
+ AUDIO_FILE)
+
+final_state = SPEECH_SUMMARY_GRAPH.run()
+print(final_state.get("answer", "No answer found."))
diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.py
index 9bde10d1..6ffbb752 100644
--- a/scrapegraphai/graphs/smart_scraper_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_graph.py
@@ -1,3 +1,6 @@
+"""
+Module for creating the smart scraper
+"""
from ..models import OpenAI
from .base_graph import BaseGraph
from ..nodes import (
@@ -6,7 +9,7 @@ from ..nodes import (
GetProbableTagsNode,
GenerateAnswerNode,
ParseHTMLNode
- )
+)
class SmartScraperGraph: