Skip to content

Python scraper based on AI

add new examples

Marco Vinciguerra1dde43cdeb1a
diff --git a/examples/azure/csv_scraper_graph_multi_azure.py b/examples/azure/csv_scraper_graph_multi_azure.py
new file mode 100644
index 00000000..c8a29829
--- /dev/null
+++ b/examples/azure/csv_scraper_graph_multi_azure.py
@@ -0,0 +1,62 @@
+"""
+Basic example of scraping pipeline using CSVScraperMultiGraph from CSV documents
+"""
+
+import os
+from dotenv import load_dotenv
+import pandas as pd
+from scrapegraphai.graphs import CSVScraperMultiGraph
+from langchain_openai import AzureChatOpenAI
+from langchain_openai import AzureOpenAIEmbeddings
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+load_dotenv()
+# ************************************************
+# Read the CSV file
+# ************************************************
+
+FILE_NAME = "inputs/username.csv"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+text = pd.read_csv(file_path)
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+llm_model_instance = AzureChatOpenAI(
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+ azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
+)
+
+embedder_model_instance = AzureOpenAIEmbeddings(
+ azure_deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"],
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+)
+graph_config = {
+ "llm": {"model_instance": llm_model_instance},
+ "embeddings": {"model_instance": embedder_model_instance}
+}
+# ************************************************
+# Create the CSVScraperMultiGraph instance and run it
+# ************************************************
+
+csv_scraper_graph = CSVScraperMultiGraph(
+ prompt="List me all the last names",
+ source=[str(text), str(text)],
+ config=graph_config
+)
+
+result = csv_scraper_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = csv_scraper_graph.get_execution_info()
+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/azure/json_scraper_multi_azure.py b/examples/azure/json_scraper_multi_azure.py
new file mode 100644
index 00000000..c6295328
--- /dev/null
+++ b/examples/azure/json_scraper_multi_azure.py
@@ -0,0 +1,40 @@
+"""
+Module for showing how JSONScraperMultiGraph multi works
+"""
+import os
+import json
+from langchain_openai import AzureChatOpenAI
+from langchain_openai import AzureOpenAIEmbeddings
+from scrapegraphai.graphs import JSONScraperMultiGraph
+
+llm_model_instance = AzureChatOpenAI(
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+ azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
+)
+
+embedder_model_instance = AzureOpenAIEmbeddings(
+ azure_deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"],
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+)
+graph_config = {
+ "llm": {"model_instance": llm_model_instance},
+ "embeddings": {"model_instance": embedder_model_instance}
+}
+FILE_NAME = "inputs/example.json"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+with open(file_path, 'r', encoding="utf-8") as file:
+ text = file.read()
+
+sources = [text, text]
+
+multiple_search_graph = JSONScraperMultiGraph(
+ prompt= "List me all the authors, title and genres of the books",
+ source= sources,
+ schema=None,
+ config=graph_config
+)
+
+result = multiple_search_graph.run()
+print(json.dumps(result, indent=4))
diff --git a/examples/azure/xml_scraper_graph_multi_azure.py b/examples/azure/xml_scraper_graph_multi_azure.py
new file mode 100644
index 00000000..e0d55bd4
--- /dev/null
+++ b/examples/azure/xml_scraper_graph_multi_azure.py
@@ -0,0 +1,64 @@
+"""
+Basic example of scraping pipeline using XMLScraperMultiGraph from XML documents
+"""
+
+import os
+from dotenv import load_dotenv
+from scrapegraphai.graphs import XMLScraperMultiGraph
+from langchain_openai import AzureChatOpenAI
+from langchain_openai import AzureOpenAIEmbeddings
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+load_dotenv()
+
+# ************************************************
+# Read the XML file
+# ************************************************
+
+FILE_NAME = "inputs/books.xml"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+with open(file_path, 'r', encoding="utf-8") as file:
+ text = file.read()
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+llm_model_instance = AzureChatOpenAI(
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+ azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"]
+)
+
+embedder_model_instance = AzureOpenAIEmbeddings(
+ azure_deployment=os.environ["AZURE_OPENAI_EMBEDDINGS_DEPLOYMENT_NAME"],
+ openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
+)
+graph_config = {
+ "llm": {"model_instance": llm_model_instance},
+ "embeddings": {"model_instance": embedder_model_instance}
+}
+
+# ************************************************
+# Create the XMLScraperMultiGraph instance and run it
+# ************************************************
+
+xml_scraper_graph = XMLScraperMultiGraph(
+ prompt="List me all the authors, title and genres of the books",
+ source=[text, text], # Pass the content of the file, not the file object
+ config=graph_config
+)
+
+result = xml_scraper_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = xml_scraper_graph.get_execution_info()
+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/bedrock/json_scraper_multi_bedrock.py b/examples/bedrock/json_scraper_multi_bedrock.py
new file mode 100644
index 00000000..5dc666b8
--- /dev/null
+++ b/examples/bedrock/json_scraper_multi_bedrock.py
@@ -0,0 +1,35 @@
+"""
+Module for showing how JSONScraperMultiGraph multi works
+"""
+import os
+import json
+from scrapegraphai.graphs import JSONScraperMultiGraph
+
+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"
+ }
+}
+FILE_NAME = "inputs/example.json"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+with open(file_path, 'r', encoding="utf-8") as file:
+ text = file.read()
+
+sources = [text, text]
+
+multiple_search_graph = JSONScraperMultiGraph(
+ prompt= "List me all the authors, title and genres of the books",
+ source= sources,
+ schema=None,
+ config=graph_config
+)
+
+result = multiple_search_graph.run()
+print(json.dumps(result, indent=4))
diff --git a/examples/huggingfacehub/csv_scraper_graph_multi_huggingfacehub.py b/examples/huggingfacehub/csv_scraper_graph_multi_huggingfacehub.py
new file mode 100644
index 00000000..4517bbe9
--- /dev/null
+++ b/examples/huggingfacehub/csv_scraper_graph_multi_huggingfacehub.py
@@ -0,0 +1,69 @@
+"""
+Basic example of scraping pipeline using CSVScraperMultiGraph from CSV documents
+"""
+
+import os
+import pandas as pd
+from scrapegraphai.graphs import CSVScraperMultiGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+from langchain_community.llms import HuggingFaceEndpoint
+from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
+
+# ************************************************
+# Read the CSV file
+# ************************************************
+
+FILE_NAME = "inputs/username.csv"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+text = pd.read_csv(file_path)
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
+
+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}
+}
+
+
+# ************************************************
+# Create the CSVScraperMultiGraph instance and run it
+# ************************************************
+
+csv_scraper_graph = CSVScraperMultiGraph(
+ prompt="List me all the last names",
+ source=[str(text), str(text)],
+ config=graph_config
+)
+
+result = csv_scraper_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = csv_scraper_graph.get_execution_info()
+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/huggingfacehub/xml_scraper_graph_multi_huggingfacehub.py b/examples/huggingfacehub/xml_scraper_graph_multi_huggingfacehub.py
new file mode 100644
index 00000000..24d6babd
--- /dev/null
+++ b/examples/huggingfacehub/xml_scraper_graph_multi_huggingfacehub.py
@@ -0,0 +1,68 @@
+"""
+Basic example of scraping pipeline using XMLScraperMultiGraph from XML documents
+"""
+
+import os
+from scrapegraphai.graphs import XMLScraperMultiGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+from langchain_community.llms import HuggingFaceEndpoint
+from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
+
+# ************************************************
+# Read the XML file
+# ************************************************
+
+FILE_NAME = "inputs/books.xml"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+with open(file_path, 'r', encoding="utf-8") as file:
+ text = file.read()
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
+
+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}
+}
+
+# ************************************************
+# Create the XMLScraperMultiGraph instance and run it
+# ************************************************
+
+xml_scraper_graph = XMLScraperMultiGraph(
+ prompt="List me all the authors, title and genres of the books",
+ source=[text, text], # Pass the content of the file, not the file object
+ config=graph_config
+)
+
+result = xml_scraper_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = xml_scraper_graph.get_execution_info()
+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/oneapi/csv_scraper_graph_multi_oneapi.py b/examples/oneapi/csv_scraper_graph_multi_oneapi.py
index e69de29b..890765df 100644
--- a/examples/oneapi/csv_scraper_graph_multi_oneapi.py
+++ b/examples/oneapi/csv_scraper_graph_multi_oneapi.py
@@ -0,0 +1,56 @@
+"""
+Basic example of scraping pipeline using CSVScraperMultiGraph from CSV documents
+"""
+
+import os
+from dotenv import load_dotenv
+import pandas as pd
+from scrapegraphai.graphs import CSVScraperMultiGraph
+from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
+
+load_dotenv()
+# ************************************************
+# Read the CSV file
+# ************************************************
+
+FILE_NAME = "inputs/username.csv"
+curr_dir = os.path.dirname(os.path.realpath(__file__))
+file_path = os.path.join(curr_dir, FILE_NAME)
+
+text = pd.read_csv(file_path)
+
+# ************************************************
+# Define the configuration for the graph
+# ************************************************
+
+graph_config = {
+ "llm": {
+ "api_key": "***************************",
+ "model": "oneapi/qwen-turbo",
+ "base_url": "http://127.0.0.1:3000/v1", # 设置 OneAPI URL
+ }
+}
+
+# ************************************************
+# Create the CSVScraperMultiGraph instance and run it
+# ************************************************
+
+csv_scraper_graph = CSVScraperMultiGraph(
+ prompt="List me all the last names",
+ source=[str(text), str(text)],
+ config=graph_config
+)
+
+result = csv_scraper_graph.run()
+print(result)
+
+# ************************************************
+# Get graph execution info
+# ************************************************
+
+graph_exec_info = csv_scraper_graph.get_execution_info()
+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/openai/xml_scraper_graph_multi_ollama.py b/examples/openai/xml_scraper_graph_multi_openai.py
similarity index 90%
rename from examples/openai/xml_scraper_graph_multi_ollama.py
rename to examples/openai/xml_scraper_graph_multi_openai.py
index e0edfaef..46633bba 100644
--- a/examples/openai/xml_scraper_graph_multi_ollama.py
+++ b/examples/openai/xml_scraper_graph_multi_openai.py
@@ -23,15 +23,17 @@ with open(file_path, 'r', encoding="utf-8") as file:
# Define the configuration for the graph
# ************************************************
+
+openai_key = os.getenv("OPENAI_APIKEY")
+
graph_config = {
"llm": {
- "api_key": "***************************",
- "model": "oneapi/qwen-turbo",
- "base_url": "http://127.0.0.1:3000/v1", # 设置 OneAPI URL
- }
+ "api_key":openai_key,
+ "model": "gpt-3.5-turbo",
+ },
+ "verbose": True,
+ "headless": False,
}
-
-
# ************************************************
# Create the XMLScraperMultiGraph instance and run it
# ************************************************