Skip to content

Python scraper based on AI

fixed example graphs and utils files

Perinim7d3190a63669
diff --git a/examples/graph_examples/custom_graph_example.py b/examples/graph_examples/custom_graph_example.py
index 63779521..8a051c5f 100644
--- a/examples/graph_examples/custom_graph_example.py
+++ b/examples/graph_examples/custom_graph_example.py
@@ -66,5 +66,5 @@ result = graph.execute({
})
# get the answer from the result
-answer = result.get("answer", "No answer found.")
-print(answer)
+result = result.get("answer", "No answer found.")
+print(result)
diff --git a/examples/graph_examples/graph_from_text_example.py b/examples/graph_examples/graph_from_text_example.py
deleted file mode 100644
index 0b6733e9..00000000
--- a/examples/graph_examples/graph_from_text_example.py
+++ /dev/null
@@ -1,60 +0,0 @@
-"""
-Example of custom graph using existing nodes
-"""
-
-import os
-from dotenv import load_dotenv
-from scrapegraphai.models import OpenAI
-from scrapegraphai.graphs import BaseGraph
-from scrapegraphai.nodes import FetchTextNode, ParseNode, RAGNode, GenerateAnswerNode
-
-load_dotenv()
-
-# Define the configuration for the language model
-openai_key = os.getenv("OPENAI_APIKEY")
-llm_config = {
- "api_key": openai_key,
- "model_name": "gpt-3.5-turbo",
- "temperature": 0,
- "streaming": True
-}
-model = OpenAI(llm_config)
-
-curr_dir = os.path.dirname(__file__)
-file_path = os.path.join(curr_dir, "text_example.txt")
-
-with open(file_path, "r", encoding="utf-8") as file:
- text = file.read()
-
-
-# define the nodes for the graph
-fetch_text_node = FetchTextNode("load_html_from_text")
-parse_document_node = ParseNode(
- doc_type="text", chunks_size=4000, node_name="parse_document")
-rag_node = RAGNode(model, "rag")
-generate_answer_node = GenerateAnswerNode(model, "generate_answer")
-
-# create the graph
-graph = BaseGraph(
- nodes={
- fetch_text_node,
- parse_document_node,
- rag_node,
- generate_answer_node
- },
- edges={
- (fetch_text_node, parse_document_node),
- (parse_document_node, rag_node),
- (rag_node, generate_answer_node)
- },
- entry_point=fetch_text_node
-)
-
-# execute the graph
-inputs = {"user_input": "Give me the name of all the news",
- "text": text}
-result = graph.execute(inputs)
-
-# get the answer from the result
-answer = result.get("answer", "No answer found.")
-print(answer)
diff --git a/examples/graph_examples/plain_html_example.txt b/examples/graph_examples/plain_html_example.txt
new file mode 100644
index 00000000..78f814ae
--- /dev/null
+++ b/examples/graph_examples/plain_html_example.txt
@@ -0,0 +1,105 @@
+<body class="fixed-top-nav " style="padding-top: 57px;">
+ <header>
+ <nav id="navbar" class="navbar navbar-light navbar-expand-sm fixed-top">
+ <div class="container">
+ <a class="navbar-brand title font-weight-lighter" href="/"><span class="font-weight-bold">Marco&nbsp;</span>Perini</a> <button class="navbar-toggler collapsed ml-auto" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar top-bar"></span> <span class="icon-bar middle-bar"></span> <span class="icon-bar bottom-bar"></span> </button>
+ <div class="collapse navbar-collapse text-right" id="navbarNav">
+ <ul class="navbar-nav ml-auto flex-nowrap">
+ <li class="nav-item "> <a class="nav-link" href="/">About</a> </li>
+ <li class="nav-item dropdown active">
+ <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Projects<span class="sr-only">(current)</span></a>
+ <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
+ <a class="dropdown-item" href="/projects/">Projects</a>
+ <div class="dropdown-divider"></div>
+ <a class="dropdown-item" href="/competitions/">Competitions</a>
+ </div>
+ </li>
+ <li class="nav-item "> <a class="nav-link" href="/cv/">CV</a> </li>
+ <li class="toggle-container"> <button id="light-toggle" title="Change theme"> <i class="fa-solid fa-moon"></i> <i class="fa-solid fa-sun"></i> </button> </li>
+ </ul>
+ </div>
+ </div>
+ </nav>
+ <progress id="progress" value="0" max="284" style="top: 57px;">
+ <div class="progress-container"> <span class="progress-bar"></span> </div>
+ </progress>
+ </header>
+ <div class="container mt-5">
+ <div class="post">
+ <header class="post-header">
+ <h1 class="post-title">Projects</h1>
+ <p class="post-description"></p>
+ </header>
+ <article>
+ <div class="projects">
+ <div class="grid" style="position: relative; height: 861.992px;">
+ <div class="grid-sizer"></div>
+ <div class="grid-item" style="position: absolute; left: 0px; top: 0px;">
+ <a href="/projects/rotary-pendulum-rl/">
+ <div class="card hoverable">
+ <figure>
+ <picture> <img src="/assets/img/rotary_pybullet.jpg" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture>
+ </figure>
+ <div class="card-body">
+ <h4 class="card-title">Rotary Pendulum RL</h4>
+ <p class="card-text">Open Source project aimed at controlling a real life rotary pendulum using RL algorithms</p>
+ <div class="row ml-1 mr-1 p-0"> </div>
+ </div>
+ </div>
+ </a>
+ </div>
+ <div class="grid-sizer"></div>
+ <div class="grid-item" style="position: absolute; left: 260px; top: 0px;">
+ <a href="https://github.com/PeriniM/DQN-SwingUp" rel="external nofollow noopener" target="_blank">
+ <div class="card hoverable">
+ <figure>
+ <picture> <img src="/assets/img/value-policy-heatmaps.jpg" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture>
+ </figure>
+ <div class="card-body">
+ <h4 class="card-title">DQN Implementation from scratch</h4>
+ <p class="card-text">Developed a Deep Q-Network algorithm to train a simple and double pendulum</p>
+ <div class="row ml-1 mr-1 p-0"> </div>
+ </div>
+ </div>
+ </a>
+ </div>
+ <div class="grid-sizer"></div>
+ <div class="grid-item" style="position: absolute; left: 0px; top: 447.414px;">
+ <a href="https://github.com/PeriniM/Multi-Agents-HAED" rel="external nofollow noopener" target="_blank">
+ <div class="card hoverable">
+ <figure>
+ <picture> <img src="/assets/img/multi_agents_haed.gif" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture>
+ </figure>
+ <div class="card-body">
+ <h4 class="card-title">Multi Agents HAED</h4>
+ <p class="card-text">University project which focuses on simulating a multi-agent system to perform environment mapping. Agents, equipped with sensors, explore and record their surroundings, considering uncertainties in their readings.</p>
+ <div class="row ml-1 mr-1 p-0"> </div>
+ </div>
+ </div>
+ </a>
+ </div>
+ <div class="grid-sizer"></div>
+ <div class="grid-item" style="position: absolute; left: 260px; top: 370.172px;">
+ <a href="/projects/wireless-esc-drone/">
+ <div class="card hoverable">
+ <figure>
+ <picture> <img src="/assets/img/wireless_esc.gif" width="auto" height="auto" alt="project thumbnail" onerror="this.onerror=null; $('.responsive-img-srcset').remove();"> </picture>
+ </figure>
+ <div class="card-body">
+ <h4 class="card-title">Wireless ESC for Modular Drones</h4>
+ <p class="card-text">Modular drone architecture proposal and proof of concept. The project received maximum grade.</p>
+ <div class="row ml-1 mr-1 p-0"> </div>
+ </div>
+ </div>
+ </a>
+ </div>
+ </div>
+ </div>
+ </article>
+ </div>
+ </div>
+ <footer class="fixed-bottom">
+ <div class="container mt-0"> © Copyright 2023 Marco Perini. Powered by <a href="https://jekyllrb.com/" target="_blank" rel="external nofollow noopener">Jekyll</a> with <a href="https://github.com/alshedivat/al-folio" rel="external nofollow noopener" target="_blank">al-folio</a> theme. Hosted by <a href="https://pages.github.com/" target="_blank" rel="external nofollow noopener">GitHub Pages</a>. </div>
+ </footer>
+ <div class="hiddendiv common"></div>
+</body>
\ No newline at end of file
diff --git a/examples/graph_examples/smart_scraper_example.py b/examples/graph_examples/smart_scraper_example.py
index 15a55a63..da0507aa 100644
--- a/examples/graph_examples/smart_scraper_example.py
+++ b/examples/graph_examples/smart_scraper_example.py
@@ -15,18 +15,14 @@ graph_config = {
"api_key": openai_key,
"model": "gpt-3.5-turbo",
},
- # "embedding_model": {
- # "api_key": openai_key,
- # "model": "gpt-3.5-turbo",
- # },
}
# Create the SmartScraperGraph instance
smart_scraper_graph = SmartScraperGraph(
prompt = "List me all the news with their description.",
- url = "https://www.ansa.it/veneto/",
+ file_source = "https://www.ansa.it/veneto/", # also accepts a local file path
config = graph_config
)
-answer = smart_scraper_graph.run()
-print(answer)
+result = smart_scraper_graph.run()
+print(result)
diff --git a/examples/graph_examples/speech_graph_example.py b/examples/graph_examples/speech_graph_example.py
index 41f2d6c8..7e571fe3 100644
--- a/examples/graph_examples/speech_graph_example.py
+++ b/examples/graph_examples/speech_graph_example.py
@@ -29,10 +29,10 @@ graph_config = {
}
speech_graph = SpeechGraph(
- prompt = "List me all the projects and generate and audio for me to listen to.",
- url = "https://perinim.github.io/projects/",
+ prompt = "Create a summary of the website",
+ file_source = "https://perinim.github.io/projects/",
config = graph_config,
)
-final_state = speech_graph.run()
-print(final_state.get("answer", "No answer found."))
+result = speech_graph.run()
+print(result.get("answer", "No answer found"))
diff --git a/examples/graph_examples/text_example.txt b/examples/graph_examples/text_example.txt
deleted file mode 100644
index 7b2c29f2..00000000
--- a/examples/graph_examples/text_example.txt
+++ /dev/null
@@ -1,63 +0,0 @@
-<body class="section-hp hpsection-page base-editorial-page base-page page basicpage"><div id="dtm_script" data-src="https://assets.adobedtm.com/launch-EN578e54a3f0c6478aa41c89022e30863c.min.js"></div>
-
-<a class="c-card c-card--CA25-m c-card--CA25-t c-card--CA25-d c-card--no-abstract-m c-card--media c-card--base" href="https://sport.sky.it/nba/nba-classifica-marcatori-ogni-epoca" data-no-autoplay-policy-article-enabled="false">
- <article class="c-card__wrapper aem_card_check_wrapper">
- <div class="c-card__content">
-
- <h2 class="c-card__title">Countdown LeBron: mancano 8 punti ai 40.000</h2>
-
-
-
-
-
-
- <div class="c-card__label-wrapper c-label-wrapper">
-
- <span class="c-label c-label--article-heading">CLASSIFICA</span>
-
- </div>
-
-
-
- <p class="c-card__abstract">Nella notte, per battere Washington in una sfida meno semplice del previsto, ai Lakers sono...</p>
-
- <div class="c-card__info">
- <time class="c-card__date" datetime="01 mar - 09:10">01 mar - 09:10
- </time>
- <span class="c-card__content-data">
- <i class="icon icon--media-outline icon--gallery-outline icon--xxsmall icon--c-neutral">
- <svg width="80" height="80" viewBox="0 0 80 80" xmlns="http://www.w3.org/2000/svg" class="icon__svg icon__svg--gallery-outline"><path d="M26.174 32.174v31.975h44.588V32.174H26.174zm-3.08-9.238h50.747A6.159 6.159 0 0 1 80 29.095v38.134a6.159 6.159 0 0 1-6.159 6.158H23.095a6.159 6.159 0 0 1-6.159-6.158V29.095a6.159 6.159 0 0 1 6.159-6.159zM9.239 55.665a4.619 4.619 0 0 1-9.238 0V16.777C0 10.825 4.825 6 10.777 6H64.08a4.619 4.619 0 1 1 0 9.238H10.777c-.85 0-1.54.69-1.54 1.54v38.887z" fill="currentColor" fill-rule="evenodd"></path></svg>
- </i>
- 26 foto
- </span>
- </div>
-
-
-
-
- <!-- end -->
- </div>
-
- <div class="c-card__img-wrapper">
- <figure class="o-aspect-ratio o-aspect-ratio--16-10 ">
- <img crossorigin="anonymous" class="c-card__img j-lazyload" alt="Countdown LeBron: mancano 8 punti ai 40.000" data-srcset="https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-desktop/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 314w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-desktop-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 628w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-tablet/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 416w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-tablet-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 832w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 375w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 750w" sizes="(max-width: 1024px) 100vw, 30vw" loading="lazy" data-src="https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg" src="https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg" srcset="https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-desktop/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 314w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-desktop-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 628w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-tablet/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 416w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-tablet-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 832w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 375w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 750w">
-
- <noscript>
- <img crossorigin="anonymous" class="c-card__img" alt="Countdown LeBron: mancano 8 punti ai 40.000" srcset="https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-desktop/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 314w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-desktop-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 628w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-tablet/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 416w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-tablet-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 832w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 375w,https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-mobile-2x/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg 750w" sizes="(max-width: 1024px) 100vw, 30vw" src="https://static.sky.it/images/skysport/it/nba/nba-classifica-marcatori-ogni-epoca/LeBron_Record_Punti.jpg.transform/card-tablet/ebcd4e23041ef9af59c046ce3e919cf9000541a9/img.jpeg"/>
- </noscript>
- </figure>
-
-
-
-
- <i class="icon icon--media icon--gallery icon--medium icon--c-primary">
- <svg width="80" height="80" viewBox="0 0 80 80" xmlns="http://www.w3.org/2000/svg" class="icon__svg icon__svg--gallery">
- <path d="M17.005 20.221h60.211c1.538 0 2.784 1.28 2.784 2.858v48.317c0 1.578-1.246 2.858-2.784 2.858H17.005c-1.537 0-2.784-1.28-2.784-2.858V23.079c0-1.578 1.247-2.858 2.784-2.858zM5.873 11.873V60.62a2.937 2.937 0 0 1-5.873 0V11.286A5.286 5.286 0 0 1 5.286 6h61.08a2.937 2.937 0 1 1 0 5.873H5.873z"></path>
- </svg>
- </i>
-
- </div>
- </article>
- </a>
-
-</body>
\ No newline at end of file
diff --git a/examples/utils/vision_speech_example.py b/examples/utils/vision_speech_example.py
index 9c86271c..5c90dc25 100644
--- a/examples/utils/vision_speech_example.py
+++ b/examples/utils/vision_speech_example.py
@@ -8,28 +8,39 @@ from scrapegraphai.models import OpenAIImageToText, OpenAITextToSpeech
from scrapegraphai.utils import save_audio_from_bytes
load_dotenv()
-
-# Define the configuration for the language model
openai_key = os.getenv("OPENAI_APIKEY")
-llm_config = {
- "api_key": openai_key,
- "model_name": "gpt-4-vision-preview",
+
+# Define the configuration for the graph
+config = {
+ "itt_model": {
+ "api_key": openai_key,
+ "model": "gpt-4-vision-preview",
+ },
+ "tts_model": {
+ "api_key": openai_key,
+ "model": "tts-1",
+ "voice": "alloy"
+ },
}
-model = OpenAIImageToText(llm_config)
-ANSWER = model.run(
- "https://raw.githubusercontent.com/VinciGit00/Scrapegraph-ai/main/docs/assets/scrapegraphai_logo.png")
-print(ANSWER)
+itt_model = OpenAIImageToText(config["itt_model"])
+img_to_text_result = itt_model.run(
+ "https://raw.githubusercontent.com/VinciGit00/Scrapegraph-ai/main/docs/assets/scrapegraphai_logo.png"
+ )
+
+print(f"Image description: {img_to_text_result}")
-text_to_speech = OpenAITextToSpeech(llm_config, model="tts-1", voice="alloy")
+tts_model = OpenAITextToSpeech(config["tts_model"])
-TEXT = "Today is a wonderful day to build something people love!"
-audio = text_to_speech.run(TEXT)
+audio = tts_model.run(
+ img_to_text_result
+ )
# Save the audio to a file
+file_name = "image_description.mp3"
curr_dir = os.path.dirname(os.path.realpath(__file__))
-file_path = os.path.join(curr_dir, "text2speech.mp3")
+output_path = os.path.join(curr_dir, file_name)
-save_audio_from_bytes(audio, file_path)
+save_audio_from_bytes(audio, output_path)
-print(f"Speech file saved to: {file_path}")
+print(f"Audio file saved to: {output_path}")
diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.py
index a847b28b..e3979ceb 100644
--- a/scrapegraphai/graphs/smart_scraper_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_graph.py
@@ -34,12 +34,13 @@ class SmartScraperGraph:
'temperature', and 'streaming'.
"""
- def __init__(self, prompt: str, url: str, config: dict):
+ def __init__(self, prompt: str, file_source: str, config: dict):
"""
Initializes the SmartScraper with a prompt, URL, and language model configuration.
"""
self.prompt = prompt
- self.url = url
+ self.file_source = file_source
+ self.input_key = "url" if "http" in file_source else "local_dir"
self.config = config
self.llm_model = self._create_llm(config["llm"])
self.graph = self._create_graph()
@@ -115,7 +116,9 @@ class SmartScraperGraph:
Returns:
str: The answer extracted from the web page, corresponding to the given prompt.
"""
- inputs = {"user_prompt": self.prompt, "url": self.url}
+
+
+ inputs = {"user_prompt": self.prompt, self.input_key: self.file_source}
final_state = self.graph.execute(inputs)
return final_state.get("answer", "No answer found.")
diff --git a/scrapegraphai/graphs/speech_graph.py b/scrapegraphai/graphs/speech_graph.py
index b97d18f5..9c1e4382 100644
--- a/scrapegraphai/graphs/speech_graph.py
+++ b/scrapegraphai/graphs/speech_graph.py
@@ -35,12 +35,13 @@ class SpeechGraph:
output_path (str): The file path where the generated MP3 should be saved.
"""
- def __init__(self, prompt: str, url: str, config: dict):
+ def __init__(self, prompt: str, file_source: str, config: dict):
"""
Initializes the SmartScraper with a prompt, URL, and language model configuration.
"""
self.prompt = prompt
- self.url = url
+ self.file_source = file_source
+ self.input_key = "url" if "http" in file_source else "local_dir"
self.llm_model = self._create_llm(config["llm"])
self.output_path = config.get("output_path", "output.mp3")
self.text_to_speech_model = OpenAITextToSpeech(config["tts_model"])
@@ -124,7 +125,7 @@ class SpeechGraph:
Returns:
str: The answer extracted from the web page, corresponding to the given prompt.
"""
- inputs = {"user_prompt": self.prompt, "url": self.url}
+ inputs = {"user_prompt": self.prompt, self.input_key: self.file_source}
final_state = self.graph.execute(inputs)
audio = final_state.get("audio", None)
diff --git a/scrapegraphai/helpers/nodes_metadata.py b/scrapegraphai/helpers/nodes_metadata.py
index 5950cf21..ac25eca2 100644
--- a/scrapegraphai/helpers/nodes_metadata.py
+++ b/scrapegraphai/helpers/nodes_metadata.py
@@ -3,8 +3,8 @@ Nodes metadata for the scrapegraphai package.
"""
nodes_metadata = {
- "FetchHTMLNode": {
- "description": "Fetches HTML content from a given URL.",
+ "FetchNode": {
+ "description": "Fetches input content from a given URL or file path.",
"type": "node",
"args": {
"url": "The URL from which to fetch HTML content."
diff --git a/scrapegraphai/nodes/fetch_node.py b/scrapegraphai/nodes/fetch_node.py
index 8a539dd7..8cad7186 100644
--- a/scrapegraphai/nodes/fetch_node.py
+++ b/scrapegraphai/nodes/fetch_node.py
@@ -66,13 +66,19 @@ class FetchNode(BaseNode):
# Fetching data from the state based on the input keys
input_data = [state[key] for key in input_keys]
- loader = AsyncHtmlLoader(input_data[0])
- document = loader.load()
- # metadata = document[0].metadata
- # document = remover(str(document[0]))
+ source = input_data[0]
+ # if it is a .txt file
+ if source.endswith(".txt"):
+ with open(source, "r") as file:
+ file_content = file.read()
+ document = Document(page_content=file_content, metadata={
+ "source": source
+ })
+
+ # if it is a URL
+ else:
+ loader = AsyncHtmlLoader(source)
+ document = loader.load()
- # state["document"] = [
- # Document(page_content=document, metadata=metadata)]
state.update({self.output[0]: document})
-
return state