Skip to content

Python scraper based on AI

fetch multiple links fix

Matteo Vedovatiea3ae1fd6d24
diff --git a/examples/openai/fetch_multiple_links.py b/examples/openai/fetch_multiple_links.py
index 53e246de..c9c07877 100644
--- a/examples/openai/fetch_multiple_links.py
+++ b/examples/openai/fetch_multiple_links.py
@@ -9,6 +9,7 @@ graph_config = {
"verbose": True,
"headless": False,
"depth": 2,
+ "only_inside_links": True,
}
search_graph = DepthSearchGraph(
diff --git a/scrapegraphai/graphs/depth_search_graph.py b/scrapegraphai/graphs/depth_search_graph.py
index a96d96a7..fa6294a0 100644
--- a/scrapegraphai/graphs/depth_search_graph.py
+++ b/scrapegraphai/graphs/depth_search_graph.py
@@ -67,7 +67,8 @@ class DepthSearchGraph(AbstractGraph):
"force": self.config.get("force", False),
"cut": self.config.get("cut", True),
"browser_base": self.config.get("browser_base"),
- "depth": self.config.get("depth", 1)
+ "depth": self.config.get("depth", 1),
+ "only_inside_links": self.config.get("only_inside_links", False)
}
)
diff --git a/scrapegraphai/nodes/fetch_node_level_k.py b/scrapegraphai/nodes/fetch_node_level_k.py
index f9fd57a8..ff329a39 100644
--- a/scrapegraphai/nodes/fetch_node_level_k.py
+++ b/scrapegraphai/nodes/fetch_node_level_k.py
@@ -105,7 +105,11 @@ class FetchNodeLevelK(BaseNode):
for _ in range(self.depth):
documents = self.obtain_content(documents, loader_kwargs)
- return {self.output_keys[0]: documents}
+ filtered_documents = [doc for doc in documents if 'document' in doc]
+
+ state.update({self.output[0]: filtered_documents})
+
+ return state
def fetch_content(self, source: str, loader_kwargs) -> Optional[str]:
if self.browser_base is not None:
@@ -144,6 +148,7 @@ class FetchNodeLevelK(BaseNode):
return full_links
def obtain_content(self, documents: List, loader_kwargs) -> List:
+ new_documents = []
for doc in documents:
source = doc['source']
if 'document' not in doc:
@@ -162,10 +167,11 @@ class FetchNodeLevelK(BaseNode):
# Check if the links are already present in other documents
for link in full_links:
# Check if any document is from the same link
- if not any(d.get('source', '') == link for d in documents):
+ if not any(d.get('source', '') == link for d in documents) and not any(d.get('source', '') == link for d in new_documents):
# Add the document
- documents.append({"source": link})
+ new_documents.append({"source": link})
+ documents.extend(new_documents)
return documents
def process_links(self, base_url: str, links: list, loader_kwargs, depth: int, current_depth: int = 1) -> dict: