Skip to content

Python scraper based on AI

add examples

Marco Vinciguerra004d03a0ff43
diff --git a/examples/bedrock/csv_scraper_bedrock.py b/examples/bedrock/csv_scraper_bedrock.py
index 1fe09d0f..f015f77b 100644
--- a/examples/bedrock/csv_scraper_bedrock.py
+++ b/examples/bedrock/csv_scraper_bedrock.py
@@ -30,6 +30,7 @@ text = pd.read_csv(file_path)
graph_config = {
"llm": {
+ "client": "client_name",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"temperature": 0.0
},
@@ -37,7 +38,6 @@ graph_config = {
"model": "bedrock/cohere.embed-multilingual-v3"
}
}
-
# ************************************************
# Create the CSVScraperGraph instance and run it
# ************************************************
diff --git a/examples/bedrock/custom_graph_bedrock.py b/examples/bedrock/custom_graph_bedrock.py
index d550b46b..45358555 100644
--- a/examples/bedrock/custom_graph_bedrock.py
+++ b/examples/bedrock/custom_graph_bedrock.py
@@ -25,6 +25,7 @@ load_dotenv()
graph_config = {
"llm": {
+ "client": "client_name",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"temperature": 0.0
},
diff --git a/examples/bedrock/json_scraper_bedrock.py b/examples/bedrock/json_scraper_bedrock.py
index ad876425..0729adfe 100644
--- a/examples/bedrock/json_scraper_bedrock.py
+++ b/examples/bedrock/json_scraper_bedrock.py
@@ -29,6 +29,7 @@ with open(file_path, 'r', encoding="utf-8") as file:
graph_config = {
"llm": {
+ "client": "client_name",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"temperature": 0.0
},
diff --git a/examples/bedrock/pdf_scraper_graph_bedrock.py b/examples/bedrock/pdf_scraper_graph_bedrock.py
new file mode 100644
index 00000000..2d61a15a
--- /dev/null
+++ b/examples/bedrock/pdf_scraper_graph_bedrock.py
@@ -0,0 +1,63 @@
+"""
+Basic example of scraping pipeline using SmartScraper
+"""
+
+import os, json
+from dotenv import load_dotenv
+from scrapegraphai.utils import prettify_exec_info
+from scrapegraphai.graphs import PDFScraperGraph
+load_dotenv()
+
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+graph_config = {
+ "llm": {
+ "client": "client_name",
+ "model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
+ "temperature": 0.0
+ },
+ "embeddings": {
+ "model": "bedrock/cohere.embed-multilingual-v3"
+ }
+}
+
+source = """
+ The Divine Comedy, Italian La Divina Commedia, original name La commedia, long narrative poem written in Italian
+ circa 1308/21 by Dante. It is usually held to be one of the world s great works of literature.
+ Divided into three major sections—Inferno, Purgatorio, and Paradiso—the narrative traces the journey of Dante
+ from darkness and error to the revelation of the divine light, culminating in the Beatific Vision of God.
+ Dante is guided by the Roman poet Virgil, who represents the epitome of human knowledge, from the dark wood
+ through the descending circles of the pit of Hell (Inferno). He then climbs the mountain of Purgatory, guided
+ by the Roman poet Statius, who represents the fulfilment of human knowledge, and is finally led by his lifelong love,
+ the Beatrice of his earlier poetry, through the celestial spheres of Paradise.
+"""
+
+schema = """
+ {
+ "type": "object",
+ "properties": {
+ "summary": {
+ "type": "string"
+ },
+ "topics": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ }
+"""
+
+pdf_scraper_graph = PDFScraperGraph(
+ prompt="Summarize the text and find the main topics",
+ source=source,
+ config=graph_config,
+ schema=schema,
+)
+result = pdf_scraper_graph.run()
+
+print(json.dumps(result, indent=4))
diff --git a/examples/bedrock/scrape_plain_text_bedrock.py b/examples/bedrock/scrape_plain_text_bedrock.py
index 5cc2067c..01bec609 100644
--- a/examples/bedrock/scrape_plain_text_bedrock.py
+++ b/examples/bedrock/scrape_plain_text_bedrock.py
@@ -30,6 +30,7 @@ with open(file_path, 'r', encoding="utf-8") as file:
graph_config = {
"llm": {
+ "client": "client_name",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"temperature": 0.0
},
diff --git a/examples/bedrock/script_generator_bedrock.py b/examples/bedrock/script_generator_bedrock.py
index 038bfb53..0d3f7d07 100644
--- a/examples/bedrock/script_generator_bedrock.py
+++ b/examples/bedrock/script_generator_bedrock.py
@@ -15,13 +15,14 @@ load_dotenv()
graph_config = {
"llm": {
+ "client": "client_name",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"temperature": 0.0
},
"embeddings": {
"model": "bedrock/cohere.embed-multilingual-v3"
},
- "library": "beautifulsoup"
+ "library": "beautifulsoup"
}
# ************************************************
diff --git a/examples/bedrock/search_graph_bedrock.py b/examples/bedrock/search_graph_bedrock.py
index 79e2c803..5ca5cfa8 100644
--- a/examples/bedrock/search_graph_bedrock.py
+++ b/examples/bedrock/search_graph_bedrock.py
@@ -14,14 +14,14 @@ load_dotenv()
graph_config = {
"llm": {
+ "client": "client_name",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"temperature": 0.0
},
"embeddings": {
- "model": "bedrock/amazon.titan-embed-text-v2:0"
+ "model": "bedrock/cohere.embed-multilingual-v3"
}
}
-
# ************************************************
# Create the SearchGraph instance and run it
# ************************************************
diff --git a/examples/bedrock/smart_scraper_bedrock.py b/examples/bedrock/smart_scraper_bedrock.py
index 4f0952ae..03394434 100644
--- a/examples/bedrock/smart_scraper_bedrock.py
+++ b/examples/bedrock/smart_scraper_bedrock.py
@@ -14,15 +14,15 @@ load_dotenv()
# Define the configuration for the graph
# ************************************************
-openai_key = os.getenv("OPENAI_APIKEY")
-
graph_config = {
"llm": {
- "api_key": openai_key,
- "model": "gpt-4o",
+ "client": "client_name",
+ "model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
+ "temperature": 0.0
},
- "verbose": True,
- "headless": False,
+ "embeddings": {
+ "model": "bedrock/cohere.embed-multilingual-v3"
+ }
}
# ************************************************
diff --git a/examples/bedrock/smart_scraper_multi_bedrock.py b/examples/bedrock/smart_scraper_multi_bedrock.py
new file mode 100644
index 00000000..7aeb71cd
--- /dev/null
+++ b/examples/bedrock/smart_scraper_multi_bedrock.py
@@ -0,0 +1,41 @@
+"""
+Basic example of scraping pipeline using SmartScraper
+"""
+
+import os, json
+from dotenv import load_dotenv
+from scrapegraphai.graphs import SmartScraperMultiGraph
+
+load_dotenv()
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+graph_config = {
+ "llm": {
+ "client": "client_name",
+ "model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
+ "temperature": 0.0
+ },
+ "embeddings": {
+ "model": "bedrock/cohere.embed-multilingual-v3"
+ }
+}
+
+# *******************************************************
+# Create the SmartScraperMultiGraph instance and run it
+# *******************************************************
+
+multiple_search_graph = SmartScraperMultiGraph(
+ prompt="Who is Marco Perini?",
+ source= [
+ "https://perinim.github.io/",
+ "https://perinim.github.io/cv/"
+ ],
+ schema=None,
+ config=graph_config
+)
+
+result = multiple_search_graph.run()
+print(json.dumps(result, indent=4))
diff --git a/examples/bedrock/smart_scraper_schema_bedrock.py b/examples/bedrock/smart_scraper_schema_bedrock.py
index 3bcb8a31..d830a373 100644
--- a/examples/bedrock/smart_scraper_schema_bedrock.py
+++ b/examples/bedrock/smart_scraper_schema_bedrock.py
@@ -33,15 +33,15 @@ schema= """
# Define the configuration for the graph
# ************************************************
-openai_key = os.getenv("OPENAI_APIKEY")
-
graph_config = {
"llm": {
- "api_key": openai_key,
- "model": "gpt-4o",
+ "client": "client_name",
+ "model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
+ "temperature": 0.0
},
- "verbose": True,
- "headless": False,
+ "embeddings": {
+ "model": "bedrock/cohere.embed-multilingual-v3"
+ }
}
# ************************************************
diff --git a/examples/bedrock/xml_scraper_bedrock.py b/examples/bedrock/xml_scraper_bedrock.py
index cb4e24bc..018a8387 100644
--- a/examples/bedrock/xml_scraper_bedrock.py
+++ b/examples/bedrock/xml_scraper_bedrock.py
@@ -28,6 +28,7 @@ with open(file_path, 'r', encoding="utf-8") as file:
graph_config = {
"llm": {
+ "client": "client_name",
"model": "bedrock/anthropic.claude-3-sonnet-20240229-v1:0",
"temperature": 0.0
},
@@ -59,4 +60,3 @@ print(prettify_exec_info(graph_exec_info))
# Save to json or csv
convert_to_csv(result, "result")
convert_to_json(result, "result")
-
diff --git a/examples/deepseek/custom_graph_deepseek.py b/examples/deepseek/custom_graph_deepseek.py
new file mode 100644
index 00000000..f73639b0
--- /dev/null
+++ b/examples/deepseek/custom_graph_deepseek.py
@@ -0,0 +1,84 @@
+"""
+Example of custom graph using Gemini Google model
+"""
+
+import os
+from dotenv import load_dotenv
+from scrapegraphai.models import Gemini
+from scrapegraphai.graphs import BaseGraph
+from scrapegraphai.nodes import FetchNode, ParseNode, RAGNode, GenerateAnswerNode
+load_dotenv()
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+deepseek_key = os.getenv("DEEPSEEK_APIKEY")
+
+graph_config = {
+ "llm": {
+ "model": "deepseek-chat",
+ "openai_api_key": deepseek_key,
+ "openai_api_base": 'https://api.deepseek.com/v1',
+ },
+ "verbose": True,
+}
+
+# ************************************************
+# Define the graph nodes
+# ************************************************
+
+llm_model = Gemini(graph_config["llm"])
+
+# define the nodes for the graph
+fetch_node = FetchNode(
+ input="url | local_dir",
+ output=["doc"],
+)
+parse_node = ParseNode(
+ input="doc",
+ output=["parsed_doc"],
+ node_config={"chunk_size": 4096}
+)
+rag_node = RAGNode(
+ input="user_prompt & (parsed_doc | doc)",
+ output=["relevant_chunks"],
+ node_config={"llm": llm_model},
+)
+generate_answer_node = GenerateAnswerNode(
+ input="user_prompt & (relevant_chunks | parsed_doc | doc)",
+ output=["answer"],
+ node_config={"llm": llm_model},
+)
+
+# ************************************************
+# Create the graph by defining the connections
+# ************************************************
+
+graph = BaseGraph(
+ nodes={
+ fetch_node,
+ parse_node,
+ rag_node,
+ generate_answer_node,
+ },
+ edges={
+ (fetch_node, parse_node),
+ (parse_node, rag_node),
+ (rag_node, generate_answer_node)
+ },
+ entry_point=fetch_node
+)
+
+# ************************************************
+# Execute the graph
+# ************************************************
+
+result, execution_info = graph.execute({
+ "user_prompt": "List me the projects with their description",
+ "url": "https://perinim.github.io/projects/"
+})
+
+# get the answer from the result
+result = result.get("answer", "No answer found.")
+print(result)
diff --git a/examples/deepseek/pdf_scraper_graph_deepseek.py b/examples/deepseek/pdf_scraper_graph_deepseek.py
new file mode 100644
index 00000000..3a0f8391
--- /dev/null
+++ b/examples/deepseek/pdf_scraper_graph_deepseek.py
@@ -0,0 +1,63 @@
+"""
+Basic example of scraping pipeline using SmartScraper
+"""
+
+import os, json
+from dotenv import load_dotenv
+from scrapegraphai.utils import prettify_exec_info
+from scrapegraphai.graphs import PDFScraperGraph
+load_dotenv()
+
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+deepseek_key = os.getenv("DEEPSEEK_APIKEY")
+
+graph_config = {
+ "llm": {
+ "model": "deepseek-chat",
+ "openai_api_key": deepseek_key,
+ "openai_api_base": 'https://api.deepseek.com/v1',
+ },
+ "verbose": True,
+}
+
+source = """
+ The Divine Comedy, Italian La Divina Commedia, original name La commedia, long narrative poem written in Italian
+ circa 1308/21 by Dante. It is usually held to be one of the world s great works of literature.
+ Divided into three major sections—Inferno, Purgatorio, and Paradiso—the narrative traces the journey of Dante
+ from darkness and error to the revelation of the divine light, culminating in the Beatific Vision of God.
+ Dante is guided by the Roman poet Virgil, who represents the epitome of human knowledge, from the dark wood
+ through the descending circles of the pit of Hell (Inferno). He then climbs the mountain of Purgatory, guided
+ by the Roman poet Statius, who represents the fulfilment of human knowledge, and is finally led by his lifelong love,
+ the Beatrice of his earlier poetry, through the celestial spheres of Paradise.
+"""
+
+schema = """
+ {
+ "type": "object",
+ "properties": {
+ "summary": {
+ "type": "string"
+ },
+ "topics": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ }
+"""
+
+pdf_scraper_graph = PDFScraperGraph(
+ prompt="Summarize the text and find the main topics",
+ source=source,
+ config=graph_config,
+ schema=schema,
+)
+result = pdf_scraper_graph.run()
+
+print(json.dumps(result, indent=4))
diff --git a/examples/deepseek/scrape_plain_text_deepseek.py b/examples/deepseek/scrape_plain_text_deepseek.py
new file mode 100644
index 00000000..d7a070d7
--- /dev/null
+++ b/examples/deepseek/scrape_plain_text_deepseek.py
@@ -0,0 +1,55 @@
+"""
+Basic example of scraping pipeline using SmartScraper from text
+"""
+
+import os
+from dotenv import load_dotenv
+from scrapegraphai.graphs import SmartScraperGraph
+from scrapegraphai.utils import prettify_exec_info
+load_dotenv()
+
+# ************************************************
+# Read the text file
+# ************************************************
+
+FILE_NAME = "inputs/plain_html_example.txt"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+# It could be also a http request using the request model
+with open(file_path, 'r', encoding="utf-8") as file:
+ text = file.read()
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+deepseek_key = os.getenv("DEEPSEEK_APIKEY")
+
+graph_config = {
+ "llm": {
+ "model": "deepseek-chat",
+ "openai_api_key": deepseek_key,
+ "openai_api_base": 'https://api.deepseek.com/v1',
+ },
+ "verbose": True,
+}
+# ************************************************
+# Create the SmartScraperGraph instance and run it
+# ************************************************
+
+smart_scraper_graph = SmartScraperGraph(
+ prompt="List me all the news with their description.",
+ source=text,
+ config=graph_config
+)
+
+result = smart_scraper_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = smart_scraper_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))
diff --git a/examples/deepseek/smart_scarper_deepseek.py b/examples/deepseek/smart_scraper_deepseek.py
similarity index 100%
rename from examples/deepseek/smart_scarper_deepseek.py
rename to examples/deepseek/smart_scraper_deepseek.py
diff --git a/examples/gemini/pdf_scraper_graph_gemini.py b/examples/gemini/pdf_scraper_graph_gemini.py
new file mode 100644
index 00000000..83e9f3e7
--- /dev/null
+++ b/examples/gemini/pdf_scraper_graph_gemini.py
@@ -0,0 +1,62 @@
+"""
+Basic example of scraping pipeline using SmartScraper
+"""
+
+import os, json
+from dotenv import load_dotenv
+from scrapegraphai.utils import prettify_exec_info
+from scrapegraphai.graphs import PDFScraperGraph
+load_dotenv()
+
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+gemini_key = os.getenv("GOOGLE_APIKEY")
+
+graph_config = {
+ "llm": {
+ "api_key": gemini_key,
+ "model": "gemini-pr",
+ },
+}
+
+
+source = """
+ The Divine Comedy, Italian La Divina Commedia, original name La commedia, long narrative poem written in Italian
+ circa 1308/21 by Dante. It is usually held to be one of the world s great works of literature.
+ Divided into three major sections—Inferno, Purgatorio, and Paradiso—the narrative traces the journey of Dante
+ from darkness and error to the revelation of the divine light, culminating in the Beatific Vision of God.
+ Dante is guided by the Roman poet Virgil, who represents the epitome of human knowledge, from the dark wood
+ through the descending circles of the pit of Hell (Inferno). He then climbs the mountain of Purgatory, guided
+ by the Roman poet Statius, who represents the fulfilment of human knowledge, and is finally led by his lifelong love,
+ the Beatrice of his earlier poetry, through the celestial spheres of Paradise.
+"""
+
+schema = """
+ {
+ "type": "object",
+ "properties": {
+ "summary": {
+ "type": "string"
+ },
+ "topics": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ }
+"""
+
+pdf_scraper_graph = PDFScraperGraph(
+ prompt="Summarize the text and find the main topics",
+ source=source,
+ config=graph_config,
+ schema=schema,
+)
+result = pdf_scraper_graph.run()
+
+print(json.dumps(result, indent=4))
diff --git a/examples/gemini/smart_scraper_multi_gemini.py b/examples/gemini/smart_scraper_multi_gemini.py
new file mode 100644
index 00000000..11c846a0
--- /dev/null
+++ b/examples/gemini/smart_scraper_multi_gemini.py
@@ -0,0 +1,39 @@
+"""
+Basic example of scraping pipeline using SmartScraper
+"""
+
+import os, json
+from dotenv import load_dotenv
+from scrapegraphai.graphs import SmartScraperMultiGraph
+
+load_dotenv()
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+gemini_key = os.getenv("GOOGLE_APIKEY")
+
+graph_config = {
+ "llm": {
+ "api_key": gemini_key,
+ "model": "gemini-pro",
+ },
+}
+
+# *******************************************************
+# Create the SmartScraperMultiGraph instance and run it
+# *******************************************************
+
+multiple_search_graph = SmartScraperMultiGraph(
+ prompt="Who is Marco Perini?",
+ source= [
+ "https://perinim.github.io/",
+ "https://perinim.github.io/cv/"
+ ],
+ schema=None,
+ config=graph_config
+)
+
+result = multiple_search_graph.run()
+print(json.dumps(result, indent=4))
diff --git a/examples/huggingfacehub/smart_scraper_schema_huggingfacehub.py b/examples/huggingfacehub/smart_scraper_schema_huggingfacehub.py
new file mode 100644
index 00000000..91adad77
--- /dev/null
+++ b/examples/huggingfacehub/smart_scraper_schema_huggingfacehub.py
@@ -0,0 +1,77 @@
+"""
+Basic example of scraping pipeline using SmartScraper using Azure OpenAI Key
+"""
+
+import os
+from dotenv import load_dotenv
+from scrapegraphai.graphs import SmartScraperGraph
+from scrapegraphai.utils import prettify_exec_info
+from langchain_community.llms import HuggingFaceEndpoint
+from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
+
+# ************************************************
+# Define the output schema for the graph
+# ************************************************
+
+schema= """
+ {
+ "Projects": [
+ "Project #":
+ {
+ "title": "...",
+ "description": "...",
+ },
+ "Project #":
+ {
+ "title": "...",
+ "description": "...",
+ }
+ ]
+ }
+"""
+
+## required environment variable in .env
+#HUGGINGFACEHUB_API_TOKEN
+load_dotenv()
+
+HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
+# ************************************************
+# Initialize the model instances
+# ************************************************
+
+repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
+
+llm_model_instance = HuggingFaceEndpoint(
+ repo_id=repo_id, max_length=128, temperature=0.5, token=HUGGINGFACEHUB_API_TOKEN
+)
+
+
+
+embedder_model_instance = HuggingFaceInferenceAPIEmbeddings(
+ api_key=HUGGINGFACEHUB_API_TOKEN, model_name="sentence-transformers/all-MiniLM-l6-v2"
+)
+
+# ************************************************
+# Create the SmartScraperGraph instance and run it
+# ************************************************
+
+graph_config = {
+ "llm": {"model_instance": llm_model_instance},
+ "embeddings": {"model_instance": embedder_model_instance}
+}
+
+smart_scraper_graph = SmartScraperGraph(
+ prompt="List me all the projects with their description",
+ source="https://perinim.github.io/projects/",
+ schema=schema,
+ config=graph_config
+)
+result = smart_scraper_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = smart_scraper_graph.get_execution_info()
+print(prettify_exec_info(graph_exec_info))