Skip to content

Python scraper based on AI

add new type of scraping through text

VinciGit00be36ab8707f9
diff --git a/examples/graph_examples/scrape_plain_text.py b/examples/graph_examples/scrape_plain_text.py
index 487c4b74..04c2dad1 100644
--- a/examples/graph_examples/scrape_plain_text.py
+++ b/examples/graph_examples/scrape_plain_text.py
@@ -17,10 +17,14 @@ graph_config = {
},
}
+
+# It could be also a http request using the request model
+text = open('plain_html_example.txt', 'r', encoding="utf-8")
+
# Create the SmartScraperGraph instance
smart_scraper_graph = SmartScraperGraph(
prompt="List me all the news with their description.",
- file_source="/Users/marcovinciguerra/Github/Scrapegraph-ai/examples/graph_examples/plain_html_example.txt",
+ file_source=str(text),
config=graph_config
)
diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.py
index d9943ee1..0bfda6fc 100644
--- a/scrapegraphai/graphs/smart_scraper_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_graph.py
@@ -41,7 +41,8 @@ class SmartScraperGraph:
"""
self.prompt = prompt
self.file_source = file_source
- self.input_key = "url" if "http" in file_source else "local_dir"
+ self.input_key = "url" if file_source.startswith(
+ "http") else "local_dir"
self.config = config
self.llm_model = self._create_llm(config["llm"])
self.graph = self._create_graph()
diff --git a/scrapegraphai/nodes/fetch_node.py b/scrapegraphai/nodes/fetch_node.py
index d9247802..bb638caa 100644
--- a/scrapegraphai/nodes/fetch_node.py
+++ b/scrapegraphai/nodes/fetch_node.py
@@ -68,12 +68,11 @@ class FetchNode(BaseNode):
input_data = [state[key] for key in input_keys]
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 not source.startswith(
+ "http"):
+ document = [Document(page_content=source, metadata={
+ "source": "local_dir"
})]
# if it is a URL