Skip to content

Python scraper based on AI

refactor examples

EURAC\marperini57a53bd6af9d
diff --git a/examples/graph_examples/custom_graph_openai.py b/examples/graph_examples/custom_graph_openai.py
index 4f982ace..48fbc9d6 100644
--- a/examples/graph_examples/custom_graph_openai.py
+++ b/examples/graph_examples/custom_graph_openai.py
@@ -7,7 +7,6 @@ from dotenv import load_dotenv
from scrapegraphai.models import OpenAI
from scrapegraphai.graphs import BaseGraph
from scrapegraphai.nodes import FetchNode, ParseNode, RAGNode, GenerateAnswerNode
-from scrapegraphai.utils import convert_to_csv, convert_to_json
load_dotenv()
openai_key = os.getenv("OPENAI_APIKEY")
@@ -68,8 +67,4 @@ result = graph.execute({
# get the answer from the result
result = result.get("answer", "No answer found.")
-print(result)
-
-# Save to json and csv
-convert_to_csv(result, "result")
-convert_to_json(result, "result")
+print(result)
\ No newline at end of file
diff --git a/examples/graph_examples/smart_scraper_example.py b/examples/graph_examples/smart_scraper_example.py
index cc9ba530..a8bc2d92 100644
--- a/examples/graph_examples/smart_scraper_example.py
+++ b/examples/graph_examples/smart_scraper_example.py
@@ -5,7 +5,6 @@ Basic example of scraping pipeline using SmartScraper
import os
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
-from scrapegraphai.utils import convert_to_csv, convert_to_json
load_dotenv()
openai_key = os.getenv("OPENAI_APIKEY")
@@ -28,7 +27,3 @@ smart_scraper_graph = SmartScraperGraph(
result = smart_scraper_graph.run()
print(result)
-
-# Save to json and csv
-convert_to_csv(result, "result")
-convert_to_json(result, "result")
diff --git a/examples/graph_examples/speech_graph_example.py b/examples/graph_examples/speech_graph_example.py
index 6c2d9785..7afac269 100644
--- a/examples/graph_examples/speech_graph_example.py
+++ b/examples/graph_examples/speech_graph_example.py
@@ -5,7 +5,6 @@ Basic example of scraping pipeline using SpeechSummaryGraph
import os
from dotenv import load_dotenv
from scrapegraphai.graphs import SpeechGraph
-from scrapegraphai.utils import convert_to_csv, convert_to_json
load_dotenv()
openai_key = os.getenv("OPENAI_APIKEY")
@@ -37,7 +36,3 @@ speech_graph = SpeechGraph(
result = speech_graph.run()
print(result.get("answer", "No answer found"))
-
-# Save to json and csv
-convert_to_csv(result, "result")
-convert_to_json(result, "result")
diff --git a/scrapegraphai/nodes/search_internet_node.py b/scrapegraphai/nodes/search_internet_node.py
index 021729f9..0b33f79b 100644
--- a/scrapegraphai/nodes/search_internet_node.py
+++ b/scrapegraphai/nodes/search_internet_node.py
@@ -95,7 +95,7 @@ class SearchInternetNode(BaseNode):
print(f"Search Query: {search_query}")
# TODO: handle multiple URLs
- answer = search_on_web(query=search_query, web_browser="Google", max_results=1)[0]
+ answer = search_on_web(query=search_query, search_engine="DuckDuckGo", max_results=1)[0]
# Update the state with the generated answer
state.update({self.output[0]: answer})
diff --git a/scrapegraphai/utils/research_web.py b/scrapegraphai/utils/research_web.py
index 373b3168..8f48adcd 100644
--- a/scrapegraphai/utils/research_web.py
+++ b/scrapegraphai/utils/research_web.py
@@ -7,12 +7,12 @@ from langchain_community.tools import DuckDuckGoSearchResults
from googlesearch import search
-def search_on_web(query: str, web_browser: str = "Google", max_results: int = 10) -> List[str]:
+def search_on_web(query: str, search_engine: str = "Google", max_results: int = 10) -> List[str]:
"""
Function that given a query it finds it on the intenet
Args:
query (str): query to search on internet
- web_browser (str, optional): type of browser, it could be DuckDuckGo or Google,
+ search_engine (str, optional): type of browser, it could be DuckDuckGo or Google,
default: Google
max_results (int, optional): maximum number of results
@@ -20,18 +20,18 @@ def search_on_web(query: str, web_browser: str = "Google", max_results: int = 10
List[str]: List of strings of web link
"""
- if web_browser == "Google":
+ if search_engine == "Google":
res = []
for url in search(query, stop=max_results):
res.append(url)
return res
- elif web_browser == "DuckDuckGo":
+ elif search_engine == "DuckDuckGo":
research = DuckDuckGoSearchResults(max_results=max_results)
res = research.run(query)
- links = re.findall(r'https?://\S+', res)
+ links = re.findall(r'https?://[^\s,\]]+', res)
return links
raise ValueError(
- "The only web browsers avaiable are DuckDuckGo or Google")
+ "The only search engines avaiable are DuckDuckGo or Google")