Skip to content

Python scraper based on AI

refactoring of remove function and implement it in the fetch node

VinciGit00ce8bc08b3ab0
diff --git a/scrapegraphai/nodes/fetch_html_node.py b/scrapegraphai/nodes/fetch_html_node.py
index 71d1aa06..7c949749 100644
--- a/scrapegraphai/nodes/fetch_html_node.py
+++ b/scrapegraphai/nodes/fetch_html_node.py
@@ -4,6 +4,8 @@ Module for fetching the HTML node
from langchain_community.document_loaders import AsyncHtmlLoader
from .base_node import BaseNode
+from ..utils.remover import remover
+
class FetchHTMLNode(BaseNode):
"""
@@ -66,6 +68,8 @@ class FetchHTMLNode(BaseNode):
loader = AsyncHtmlLoader(url)
document = loader.load()
+ document = remover(document, only_body=True)
+
state["document"] = document
return state
diff --git a/scrapegraphai/utils/remover.py b/scrapegraphai/utils/remover.py
index add1a973..36bf5ada 100644
--- a/scrapegraphai/utils/remover.py
+++ b/scrapegraphai/utils/remover.py
@@ -1,40 +1,32 @@
"""
Module for removing the unused html tags
"""
+from bs4 import BeautifulSoup
-def remover(file: str, only_body: bool = False) -> str:
+def remover(html_content: str) -> str:
"""
- This function elaborates the HTML file and remove all the not necessary tag
+ This function processes the HTML content, removes unnecessary tags,
+ and retrieves the title and body content.
Parameters:
- file (str): the file to parse
+ html_content (str): the HTML content to parse
Returns:
- str: the parsed file
+ str: the parsed title followed by the body content without script tags
"""
- res = ""
+ soup = BeautifulSoup(html_content, 'html.parser')
- if only_body:
- is_body = True
- else:
- is_body = False
+ # Estrai il titolo
+ title_tag = soup.find('title')
+ title = title_tag.get_text() if title_tag else ""
- for elem in file.splitlines():
- if "<title>" in elem:
- res = res + elem
+ # Rimuovi i tag <script> in tutto il documento
+ [script.extract() for script in soup.find_all('script')]
- if "<body>" in elem:
- is_body = True
+ # Estrai il corpo del documento
+ body_content = soup.find('body')
+ body = str(body_content) if body_content else ""
- if "</body>" in elem:
- break
-
- if "<script>" in elem:
- continue
-
- if is_body:
- res = res + elem
-
- return res.replace("\\n", "")
+ return title + body