Skip to content

Python scraper based on AI

add trigger

Marco Vinciguerra7af411aa99ab
diff --git a/scrapegraphai/graphs/script_creator_graph.py b/scrapegraphai/graphs/script_creator_graph.py
index 83bef2ab..b10c2baa 100644
--- a/scrapegraphai/graphs/script_creator_graph.py
+++ b/scrapegraphai/graphs/script_creator_graph.py
@@ -64,7 +64,7 @@ class ScriptCreatorGraph(AbstractGraph):
"""
fetch_node = FetchNode(
- input="url | local_dir",
+ input="url_for_script | local_dir",
output=["doc", "link_urls", "img_urls"],
)
parse_node = ParseNode(
diff --git a/scrapegraphai/graphs/smart_scraper_graph.py b/scrapegraphai/graphs/smart_scraper_graph.py
index cfbfc000..af6dbcea 100644
--- a/scrapegraphai/graphs/smart_scraper_graph.py
+++ b/scrapegraphai/graphs/smart_scraper_graph.py
@@ -62,9 +62,10 @@ class SmartScraperGraph(AbstractGraph):
BaseGraph: A graph instance representing the web scraping workflow.
"""
fetch_node = FetchNode(
- input="url | local_dir",
+ input="url_for_scraping | local_dir",
output=["doc", "link_urls", "img_urls"],
node_config={
+ "llm_model": self.llm_model,
"loader_kwargs": self.config.get("loader_kwargs", {}),
}
)
diff --git a/scrapegraphai/nodes/fetch_node.py b/scrapegraphai/nodes/fetch_node.py
index f38cdfb9..e33d1c9a 100644
--- a/scrapegraphai/nodes/fetch_node.py
+++ b/scrapegraphai/nodes/fetch_node.py
@@ -14,6 +14,7 @@ from ..docloaders import ChromiumLoader
from ..utils.convert_to_md import convert_to_md
from ..utils.logging import get_logger
from .base_node import BaseNode
+from ..models import OpenAI
class FetchNode(BaseNode):
@@ -57,6 +58,12 @@ class FetchNode(BaseNode):
self.loader_kwargs = (
{} if node_config is None else node_config.get("loader_kwargs", {})
)
+ self.llm_model = (
+ {} if node_config is None else node_config.get("llm_model", {})
+ )
+ self.force = (
+ {} if node_config is None else node_config.get("force", {})
+ )
def execute(self, state):
"""
@@ -136,7 +143,12 @@ class FetchNode(BaseNode):
self.logger.info(f"--- (Fetching HTML from: {source}) ---")
if not source.strip():
raise ValueError("No HTML body content found in the local source.")
- parsed_content = convert_to_md(source)
+
+ parsed_content = source
+
+ if isinstance(self.llm_model, OpenAI) and self.input == "-----" or self.force:
+ parsed_content = convert_to_md(source)
+
compressed_document = [
Document(page_content=parsed_content, metadata={"source": "local_dir"})
]
@@ -147,7 +159,11 @@ class FetchNode(BaseNode):
if response.status_code == 200:
if not response.text.strip():
raise ValueError("No HTML body content found in the response.")
- parsed_content = convert_to_md(source)
+
+ parsed_content = source
+
+ if isinstance(self.llm_model, OpenAI) and self.input == "-----" or self.force:
+ parsed_content = convert_to_md(source)
compressed_document = [Document(page_content=parsed_content)]
else:
self.logger.warning(
@@ -166,8 +182,10 @@ class FetchNode(BaseNode):
if not document or not document[0].page_content.strip():
raise ValueError("No HTML body content found in the document fetched by ChromiumLoader.")
+ parsed_content = document[0].page_content
- parsed_content = convert_to_md(document[0].page_content)
+ if isinstance(self.llm_model, OpenAI) and self.input == "-----" or self.force:
+ parsed_content = convert_to_md(document[0].page_content)
compressed_document = [
Document(page_content=parsed_content, metadata={"source": "html file"})
diff --git a/scrapegraphai/nodes/generate_answer_node.py b/scrapegraphai/nodes/generate_answer_node.py
index 029f0a44..dddc9f60 100644
--- a/scrapegraphai/nodes/generate_answer_node.py
+++ b/scrapegraphai/nodes/generate_answer_node.py
@@ -101,7 +101,7 @@ class GenerateAnswerNode(BaseNode):
"format_instructions": format_instructions})
chain = prompt | self.llm_model | output_parser
answer = chain.invoke({"question": user_prompt})
-
+
else:
prompt = PromptTemplate(
template=template_chunks,
diff --git a/scrapegraphai/utils/convert_to_md.py b/scrapegraphai/utils/convert_to_md.py
index 977ec581..609643bf 100644
--- a/scrapegraphai/utils/convert_to_md.py
+++ b/scrapegraphai/utils/convert_to_md.py
@@ -4,10 +4,9 @@ convert_to_md modul
import html2text
import mdformat
from trafilatura import extract
-from markdownify import markdownify
-import pyhtml2md
-def convert_to_md(html, provider="local"):
+
+def convert_to_md(html):
""" Convert HTML to Markdown.
This function uses the html2text library to convert the provided HTML content to Markdown
format.
@@ -22,12 +21,6 @@ def convert_to_md(html, provider="local"):
'This is a paragraph.\n\n# This is a heading.'
Note: All the styles and links are ignored during the conversion. """
- if provider == "openai":
- converter = html2text.HTML2Text()
- formatted = converter.handle(html)
- a = mdformat.text(formatted)
- else:
- a = extract(filecontent=html,include_images=True, include_links=True, include_tables=True, output_format="markdown")
- b = markdownify(html, keep_inline_images_in=['td', 'th', 'a', 'figure'],)
- c = pyhtml2md.convert(html)
- return a
+
+ return extract(filecontent=html,include_images=True,
+ include_links=True, include_tables=True, output_format="markdown")