Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
subtrees implementation
EURAC\marperinic927f707e92c
diff --git a/scrapegraphai/utils/aaa.py b/scrapegraphai/utils/aaa.pyindex 8ce68423..b3ba2887 100644--- a/scrapegraphai/utils/aaa.py+++ b/scrapegraphai/utils/aaa.py@@ -4,17 +4,19 @@ from langchain_community.document_loaders import AsyncHtmlLoaderimport timeclass TreeNode:- def __init__(self, value=None, attributes=None, children=None, parent=None):+ def __init__(self, value=None, attributes=None, children=None, parent=None, depth=0):self.value = valueself.attributes = attributes if attributes is not None else {}self.children = children if children is not None else []self.parent = parent+ self.depth = depthself.leads_to_text = False # Initialize the flag as Falseself.root_path = self._compute_root_path()self.closest_fork_path = self._compute_fork_path()def add_child(self, child_node):child_node.parent = self+ child_node.depth = self.depth + 1self.children.append(child_node)child_node.update_paths()self.update_leads_to_text() # Update this node if the child leads to text@@ -48,9 +50,18 @@ class TreeNode:current = current.parentpath.append(current.value) # Add the fork or root nodereturn '>'.join(reversed(path))-++ def get_subtrees(self):+ # This method finds and returns subtrees rooted at this node and all descendant forks+ subtrees = []+ if self.is_fork:+ subtrees.append(Tree(root=self))+ for child in self.children:+ subtrees.extend(child.get_subtrees())+ return subtrees+def __repr__(self):- return f"TreeNode(value={self.value}, leads_to_text={self.leads_to_text}, root_path={self.root_path}, closest_fork_path={self.closest_fork_path})"+ return f"TreeNode(value={self.value}, leads_to_text={self.leads_to_text}, depth={self.depth}, root_path={self.root_path}, closest_fork_path={self.closest_fork_path})"@propertydef is_fork(self):@@ -72,6 +83,10 @@ class Tree:_traverse(child)_traverse(self.root)+ def get_subtrees(self):+ # Retrieves all subtrees rooted at fork nodes+ return self.root.get_subtrees() if self.root else []+def __repr__(self):return f"Tree(root={self.root})"@@ -89,7 +104,6 @@ class DOMTree(Tree):elif isinstance(child, NavigableString):text = child.strip()if text:- # Create a text node with value 'text' and the actual content under 'content' keytree_node.add_child(TreeNode(value='text', attributes={'content': text}))elif isinstance(child, Tag):new_node = TreeNode(value=child.name, attributes=child.attrs)@@ -98,14 +112,20 @@ class DOMTree(Tree):# Usage example:-loader = AsyncHtmlLoader('https://www.mymovies.it/cinema/roma/')+loader = AsyncHtmlLoader('https://github.com/PeriniM')document = loader.load()html_content = document[0].page_contentcurr_time = time.time()# Instantiate a DOMTree with HTML contentdom_tree = DOMTree(html_content)+subtrees = dom_tree.get_subtrees() # Retrieve subtrees rooted at fork nodes+print(f"Time taken to build DOM tree: {time.time() - curr_time:.2f} seconds")+# Optionally, traverse each subtree+for subtree in subtrees:+ print("Subtree rooted at:", subtree.root.value)+ # subtree.traverse(lambda node: print(node))# Traverse the DOMTree and print each node# dom_tree.traverse(lambda node: print(node))