Skip to content

Python scraper based on AI

refactoring of the code

Marco Vinciguerradb4b7cd3d1de
diff --git a/scrapegraphai/graphs/abstract_graph.py b/scrapegraphai/graphs/abstract_graph.py
index 99c13886..c693400c 100644
--- a/scrapegraphai/graphs/abstract_graph.py
+++ b/scrapegraphai/graphs/abstract_graph.py
@@ -128,7 +128,8 @@ class AbstractGraph(ABC):
if requests_per_second is not None:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
- llm_params["rate_limiter"] = InMemoryRateLimiter(requests_per_second=requests_per_second)
+ llm_params["rate_limiter"] = InMemoryRateLimiter(
+ requests_per_second=requests_per_second)
if max_retries is not None:
llm_params["max_retries"] = max_retries
diff --git a/scrapegraphai/graphs/base_graph.py b/scrapegraphai/graphs/base_graph.py
index 5fa9ff34..45953b2f 100644
--- a/scrapegraphai/graphs/base_graph.py
+++ b/scrapegraphai/graphs/base_graph.py
@@ -59,7 +59,7 @@ class BaseGraph:
# raise a warning if the entry point is not the first node in the list
warnings.warn(
"Careful! The entry point node is different from the first node in the graph.")
-
+
self._set_conditional_node_edges()
# Burr configuration
@@ -89,11 +89,9 @@ class BaseGraph:
"""
for node in self.nodes:
if node.node_type == 'conditional_node':
- # Find outgoing edges from this ConditionalNode
outgoing_edges = [(from_node, to_node) for from_node, to_node in self.raw_edges if from_node.node_name == node.node_name]
if len(outgoing_edges) != 2:
raise ValueError(f"ConditionalNode '{node.node_name}' must have exactly two outgoing edges.")
- # Assign true_node_name and false_node_name
node.true_node_name = outgoing_edges[0][1].node_name
node.false_node_name = outgoing_edges[1][1].node_name
diff --git a/scrapegraphai/graphs/code_generator_graph.py b/scrapegraphai/graphs/code_generator_graph.py
index ed884637..b8329f18 100644
--- a/scrapegraphai/graphs/code_generator_graph.py
+++ b/scrapegraphai/graphs/code_generator_graph.py
@@ -99,6 +99,7 @@ class CodeGeneratorGraph(AbstractGraph):
"schema": self.schema,
}
)
+
prompt_refier_node = PromptRefinerNode(
input="user_prompt",
output=["refined_prompt"],
@@ -108,6 +109,7 @@ class CodeGeneratorGraph(AbstractGraph):
"schema": self.schema
}
)
+
html_analyzer_node = HtmlAnalyzerNode(
input="refined_prompt & original_html",
output=["html_info", "reduced_html"],
@@ -118,6 +120,7 @@ class CodeGeneratorGraph(AbstractGraph):
"reduction": self.config.get("reduction", 0)
}
)
+
generate_code_node = GenerateCodeNode(
input="user_prompt & refined_prompt & html_info & reduced_html & answer",
output=["generated_code"],
diff --git a/scrapegraphai/graphs/csv_scraper_graph.py b/scrapegraphai/graphs/csv_scraper_graph.py
index d7854de9..a4165a9d 100644
--- a/scrapegraphai/graphs/csv_scraper_graph.py
+++ b/scrapegraphai/graphs/csv_scraper_graph.py
@@ -59,6 +59,7 @@ class CSVScraperGraph(AbstractGraph):
"""
Creates the graph of nodes representing the workflow for web scraping.
"""
+
fetch_node = FetchNode(
input="csv | csv_dir",
output=["doc"],
@@ -90,6 +91,7 @@ class CSVScraperGraph(AbstractGraph):
"""
Executes the web scraping process and returns the answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, self.input_key: self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
diff --git a/scrapegraphai/graphs/csv_scraper_multi_graph.py b/scrapegraphai/graphs/csv_scraper_multi_graph.py
index e7a028f3..c3463c40 100644
--- a/scrapegraphai/graphs/csv_scraper_multi_graph.py
+++ b/scrapegraphai/graphs/csv_scraper_multi_graph.py
@@ -95,6 +95,7 @@ class CSVScraperMultiGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, "jsons": self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
diff --git a/scrapegraphai/graphs/document_scraper_multi_graph.py b/scrapegraphai/graphs/document_scraper_multi_graph.py
index aabed189..8e850eb1 100644
--- a/scrapegraphai/graphs/document_scraper_multi_graph.py
+++ b/scrapegraphai/graphs/document_scraper_multi_graph.py
@@ -94,6 +94,7 @@ class DocumentScraperMultiGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, "xmls": self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
diff --git a/scrapegraphai/graphs/json_scraper_multi_graph.py b/scrapegraphai/graphs/json_scraper_multi_graph.py
index 6e5434f0..086651af 100644
--- a/scrapegraphai/graphs/json_scraper_multi_graph.py
+++ b/scrapegraphai/graphs/json_scraper_multi_graph.py
@@ -96,6 +96,7 @@ class JSONScraperMultiGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, "jsons": self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
diff --git a/scrapegraphai/graphs/omni_scraper_graph.py b/scrapegraphai/graphs/omni_scraper_graph.py
index 55af447f..c15bc065 100644
--- a/scrapegraphai/graphs/omni_scraper_graph.py
+++ b/scrapegraphai/graphs/omni_scraper_graph.py
@@ -62,6 +62,7 @@ class OmniScraperGraph(AbstractGraph):
Returns:
BaseGraph: A graph instance representing the web scraping workflow.
"""
+
fetch_node = FetchNode(
input="url | local_dir",
output=["doc"],
@@ -69,6 +70,7 @@ class OmniScraperGraph(AbstractGraph):
"loader_kwargs": self.config.get("loader_kwargs", {}),
}
)
+
parse_node = ParseNode(
input="doc & (url | local_dir)",
output=["parsed_doc", "link_urls", "img_urls"],
@@ -78,6 +80,7 @@ class OmniScraperGraph(AbstractGraph):
"llm_model": self.llm_model
}
)
+
image_to_text_node = ImageToTextNode(
input="img_urls",
output=["img_desc"],
diff --git a/scrapegraphai/graphs/omni_search_graph.py b/scrapegraphai/graphs/omni_search_graph.py
index 41d35ab7..f1f90f07 100644
--- a/scrapegraphai/graphs/omni_search_graph.py
+++ b/scrapegraphai/graphs/omni_search_graph.py
@@ -59,13 +59,6 @@ class OmniSearchGraph(AbstractGraph):
BaseGraph: A graph instance representing the web scraping and searching workflow.
"""
- # omni_scraper_instance = OmniScraperGraph(
- # prompt="",
- # source="",
- # config=self.copy_config,
- # schema=self.copy_schema
- # )
-
search_internet_node = SearchInternetNode(
input="user_prompt",
output=["urls"],
@@ -115,6 +108,7 @@ class OmniSearchGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt}
self.final_state, self.execution_info = self.graph.execute(inputs)
diff --git a/scrapegraphai/graphs/script_creator_multi_graph.py b/scrapegraphai/graphs/script_creator_multi_graph.py
index de1ab6f7..1d782d0f 100644
--- a/scrapegraphai/graphs/script_creator_multi_graph.py
+++ b/scrapegraphai/graphs/script_creator_multi_graph.py
@@ -93,6 +93,7 @@ class ScriptCreatorMultiGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, "urls": self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
return self.final_state.get("merged_script", "Failed to generate the script.")
diff --git a/scrapegraphai/graphs/search_graph.py b/scrapegraphai/graphs/search_graph.py
index 83e2532f..29a91376 100644
--- a/scrapegraphai/graphs/search_graph.py
+++ b/scrapegraphai/graphs/search_graph.py
@@ -110,6 +110,7 @@ class SearchGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt}
self.final_state, self.execution_info = self.graph.execute(inputs)
@@ -126,4 +127,5 @@ class SearchGraph(AbstractGraph):
Returns:
List[str]: A list of URLs considered during the search.
"""
+
return self.considered_urls
diff --git a/scrapegraphai/graphs/search_link_graph.py b/scrapegraphai/graphs/search_link_graph.py
index 9df04871..1a83feb1 100644
--- a/scrapegraphai/graphs/search_link_graph.py
+++ b/scrapegraphai/graphs/search_link_graph.py
@@ -47,6 +47,7 @@ class SearchLinkGraph(AbstractGraph):
Returns:
BaseGraph: A graph instance representing the web scraping workflow.
"""
+
fetch_node = FetchNode(
input="url| local_dir",
output=["doc"],
diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.py
index 60407624..bc85bb00 100644
--- a/scrapegraphai/graphs/smart_scraper_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_graph.py
@@ -58,6 +58,7 @@ class SmartScraperGraph(AbstractGraph):
Returns:
BaseGraph: A graph instance representing the web scraping workflow.
"""
+
fetch_node = FetchNode(
input="url| local_dir",
output=["doc"],
diff --git a/scrapegraphai/graphs/smart_scraper_multi_concat_graph.py b/scrapegraphai/graphs/smart_scraper_multi_concat_graph.py
index 0bd84a55..f7a6bee4 100644
--- a/scrapegraphai/graphs/smart_scraper_multi_concat_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_multi_concat_graph.py
@@ -41,7 +41,7 @@ class SmartScraperMultiConcatGraph(AbstractGraph):
... )
>>> result = search_graph.run()
"""
-
+
def __init__(self, prompt: str, source: List[str],
config: dict, schema: Optional[BaseModel] = None):
@@ -123,6 +123,7 @@ class SmartScraperMultiConcatGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, "urls": self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
diff --git a/scrapegraphai/graphs/smart_scraper_multi_graph.py b/scrapegraphai/graphs/smart_scraper_multi_graph.py
index 5dff3277..8d0063a3 100644
--- a/scrapegraphai/graphs/smart_scraper_multi_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_multi_graph.py
@@ -96,6 +96,7 @@ class SmartScraperMultiGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, "urls": self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)
diff --git a/scrapegraphai/graphs/xml_scraper_multi_graph.py b/scrapegraphai/graphs/xml_scraper_multi_graph.py
index 10887c6b..42f795c6 100644
--- a/scrapegraphai/graphs/xml_scraper_multi_graph.py
+++ b/scrapegraphai/graphs/xml_scraper_multi_graph.py
@@ -93,6 +93,7 @@ class XMLScraperMultiGraph(AbstractGraph):
Returns:
str: The answer to the prompt.
"""
+
inputs = {"user_prompt": self.prompt, "xmls": self.source}
self.final_state, self.execution_info = self.graph.execute(inputs)