Skip to content

Python scraper based on AI

remove asdt

VinciGit0040260d8d25f1
diff --git a/scrapegraphai/asdt/__init__.py b/scrapegraphai/asdt/__init__.py
deleted file mode 100644
index 539534d6..00000000
--- a/scrapegraphai/asdt/__init__.py
+++ /dev/null
@@ -1,5 +0,0 @@
-"""
- __init__.py file for asdt module.
-"""
-
-from .dom_tree import DOMTree
diff --git a/scrapegraphai/asdt/dom_tree.py b/scrapegraphai/asdt/dom_tree.py
deleted file mode 100644
index 50b2e179..00000000
--- a/scrapegraphai/asdt/dom_tree.py
+++ /dev/null
@@ -1,52 +0,0 @@
-from bs4 import BeautifulSoup, Comment, NavigableString, Tag
-from .tree import Tree
-from .tree_node import TreeNode
-
-class DOMTree(Tree):
- def __init__(self, html_content):
- super().__init__()
- self.root = TreeNode('document')
- self.build_dom_tree(BeautifulSoup(html_content, 'html.parser'), self.root)
-
- def build_dom_tree(self, soup_node, tree_node):
- for child in soup_node.children:
- if isinstance(child, Comment):
- continue # Skip comments
- elif isinstance(child, NavigableString):
- text = child.strip()
- if text:
- new_node = TreeNode(value='text', attributes={'content': text})
- tree_node.add_child(new_node)
- new_node.finalize_node()
- elif isinstance(child, Tag):
- new_node = TreeNode(value=child.name, attributes=child.attrs)
- tree_node.add_child(new_node)
- self.build_dom_tree(child, new_node)
-
- def collect_text_nodes(self, exclude_script=True):
- texts = []
- metadatas = []
-
- def collect(node):
- # If node is a text node, collect its data
- if node.value == 'text':
- texts.append(node.attributes['content'])
- metadatas.append({
- 'root_path': node.root_path,
- 'closest_fork_path': node.closest_fork_path
- })
-
- # Traverse the DOM tree to collect text nodes and their metadata
- def traverse_for_text(node):
- # Skip traversal into script tags, but continue for other nodes
- if exclude_script and node.value == 'script':
- return # Skip script tags
-
- if node.leads_to_text or node.value == 'text':
- collect(node)
- for child in node.children:
- traverse_for_text(child)
-
- traverse_for_text(self.root)
- return texts, metadatas
-
diff --git a/scrapegraphai/asdt/tree.py b/scrapegraphai/asdt/tree.py
deleted file mode 100644
index be95f8e6..00000000
--- a/scrapegraphai/asdt/tree.py
+++ /dev/null
@@ -1,98 +0,0 @@
-from graphviz import Digraph
-
-class Tree:
- def __init__(self, root=None):
- self.root = root
-
- def traverse(self, visit_func):
- def _traverse(node):
- if node:
- visit_func(node)
- for child in node.children:
- _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 generate_subtree_dicts(self):
- subtree_dicts = []
-
- def aggregate_text_under_fork(fork_node):
- text_aggregate = {
- "content": [],
- "path_to_fork": ""
- }
- for child in fork_node.children:
- if child.value == 'text':
- text_aggregate["content"].append(child.attributes['content'])
- elif child.is_fork:
- continue
- else:
- for sub_child in child.children:
- text_aggregate["content"].append(sub_child.attributes)
-
- text_aggregate["path_to_fork"] = fork_node.closest_fork_path
- return text_aggregate
-
- def process_node(node):
- if node.is_fork:
- texts = aggregate_text_under_fork(node)
- if texts["content"]: # Only add if there's text content
- subtree_dicts.append({
- node.value: {
- "text": texts,
- "path_to_fork": texts["path_to_fork"],
- }
- })
- for child in node.children:
- process_node(child)
-
- process_node(self.root)
- return subtree_dicts
-
- def visualize(self, exclude_tags = ['script']):
- def add_nodes_edges(tree_node, graph):
- if tree_node:
- # Skip excluded tags
- if tree_node.value in exclude_tags:
- return
-
- # Format node label to include attributes
- attr_str = None
- label = f"{tree_node.value}\n[{attr_str}]" if attr_str else tree_node.value
- # Determine color based on node properties
- if tree_node.value == 'text':
- color = 'red' # Text nodes
- elif tree_node.is_fork:
- color = 'green' # Fork nodes
- elif tree_node.leads_to_text:
- color = 'lightblue2' # Nodes leading to text
- else:
- color = 'white' # Nodes that do not lead to text and are not forks
-
- # Customize node appearance
- graph.node(name=str(id(tree_node)), label=label,
- fontsize='12', shape='ellipse', color=color, fontcolor='black')
-
- if tree_node.parent:
- graph.edge(str(id(tree_node.parent)), str(id(tree_node)), fontsize='10')
-
- for child in tree_node.children:
- add_nodes_edges(child, graph)
-
-
- # Initialize Digraph, set graph and node attributes
- graph = Digraph()
- # graph.attr(size='10,10', dpi='300') # Set higher DPI for better image resolution
- graph.attr('node', style='filled', fontname='Helvetica')
- graph.attr('edge', fontname='Helvetica')
-
- add_nodes_edges(self.root, graph)
- graph.render('tree_visualization', view=True, format='svg') # Change format to SVG for vectorized output
-
- return graph
-
- def __repr__(self):
- return f"Tree(root={self.root})"
\ No newline at end of file
diff --git a/scrapegraphai/asdt/tree_node.py b/scrapegraphai/asdt/tree_node.py
deleted file mode 100644
index 636cb5c1..00000000
--- a/scrapegraphai/asdt/tree_node.py
+++ /dev/null
@@ -1,114 +0,0 @@
-from .tree import Tree
-
-class TreeNode:
- def __init__(self, value=None, attributes=None, children=None, parent=None, depth=0):
- self.value = value
- self.attributes = attributes if attributes is not None else {}
- self.children = children if children is not None else []
- self.parent = parent
- self.depth = depth
- # Flag to track if the subtree leads to text
- self.leads_to_text = False
- # Flags to track if the subtree has a direct leaf node
- self.has_direct_leaves = False
- self.root_path = self._compute_root_path()
- self.closest_fork_path = self._compute_fork_path()
- self.structure_hash = None
- self.content_hash = None
-
- def add_child(self, child_node):
- child_node.parent = self
- child_node.depth = self.depth + 1
- self.children.append(child_node)
- child_node.update_paths()
- self.update_leads_to_text()
- self.update_hashes() # Update hashes when the structure changes
-
- def update_hashes(self):
- self.structure_hash = self.hash_subtree_structure(self)
- self.content_hash = self.hash_subtree_content(self)
-
- def update_paths(self):
- self.root_path = self._compute_root_path()
- self.closest_fork_path = self._compute_fork_path()
-
- def update_leads_to_text(self):
- # Check if any child leads to text or is a text node
- if any(child.value == 'text' or child.leads_to_text for child in self.children):
- self.leads_to_text = True
- # Update the flag up the tree
- if self.parent and not self.parent.leads_to_text:
- self.parent.update_leads_to_text()
-
- def _compute_root_path(self):
- path = []
- current = self
- while current.parent:
- path.append(current.value)
- current = current.parent
- path.append('root') # Append 'root' to start of the path
- return '>'.join(reversed(path))
-
- def _compute_fork_path(self):
- path = []
- current = self
- while current.parent and len(current.parent.children) == 1:
- path.append(current.value)
- current = current.parent
- path.append(current.value) # Add the fork or root node
- return '>'.join(reversed(path))
-
- def finalize_node(self):
- if self.is_text and self.is_leaf:
- self.update_direct_leaves_flag()
-
- def update_direct_leaves_flag(self):
- ancestor = self.parent
- while ancestor and len(ancestor.children) == 1:
- ancestor = ancestor.parent
- if ancestor and ancestor.is_fork:
- ancestor.has_direct_leaves = True
-
- def get_subtrees(self, direct_leaves=False):
- # This method finds and returns subtrees rooted at this node and all descendant forks
- # Optionally filters to include only those with direct leaves beneath fork nodes
- subtrees = []
- if self.is_fork and (not direct_leaves or self.has_direct_leaves):
- subtrees.append(Tree(root=self))
- for child in self.children:
- subtrees.extend(child.get_subtrees(direct_leaves=direct_leaves))
- return subtrees
-
- def hash_subtree_structure(self, node):
- """ Recursively generate a hash for the subtree structure. """
- if node.is_leaf:
- return hash((node.value,)) # Simple hash for leaf nodes
- child_hashes = tuple(self.hash_subtree_structure(child) for child in node.children)
- return hash((node.value, child_hashes))
-
- def hash_subtree_content(self, node):
- """ Generate a hash based on the concatenated text of the subtree. """
- text_content = self.get_all_text(node).lower().strip()
- return hash(text_content)
-
- def get_all_text(self, node):
- """ Recursively get all text from a node and its descendants. """
- text = node.attributes.get('content', '') if node.value == 'text' else ''
- for child in node.children:
- text += self.get_all_text(child)
- return text
-
- def __repr__(self):
- return f"TreeNode(value={self.value}, leads_to_text={self.leads_to_text}, is_fork={self.is_fork})"
-
- @property
- def is_fork(self):
- return len(self.children) > 1
-
- @property
- def is_leaf(self):
- return len(self.children) == 0
-
- @property
- def is_text(self):
- return self.value == 'text'
\ No newline at end of file
diff --git a/scrapegraphai/graphs/__init__.py b/scrapegraphai/graphs/__init__.py
index 10eb6d8e..15f4a4ec 100644
--- a/scrapegraphai/graphs/__init__.py
+++ b/scrapegraphai/graphs/__init__.py
@@ -15,4 +15,3 @@ from .csv_scraper_graph import CSVScraperGraph
from .pdf_scraper_graph import PDFScraperGraph
from .omni_scraper_graph import OmniScraperGraph
from .omni_search_graph import OmniSearchGraph
-from .turbo_scraper import TurboScraperGraph