mirror of
https://github.com/VinciGit00/Scrapegraph-ai.git
synced 2026-07-12 21:01:56 +08:00
fix: fix robot node
This commit is contained in:
parent
a87702f107
commit
2419003999
@ -11,10 +11,15 @@ from scrapegraphai.nodes import RobotsNode
|
|||||||
|
|
||||||
graph_config = {
|
graph_config = {
|
||||||
"llm": {
|
"llm": {
|
||||||
"model_name": "ollama/llama3",
|
"model": "ollama/llama3",
|
||||||
"temperature": 0,
|
"temperature": 0,
|
||||||
"streaming": True
|
"streaming": True
|
||||||
},
|
},
|
||||||
|
"embeddings": {
|
||||||
|
"model": "ollama/nomic-embed-text",
|
||||||
|
"temperature": 0,
|
||||||
|
# "base_url": "http://localhost:11434", # set ollama URL arbitrarily
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# ************************************************
|
# ************************************************
|
||||||
|
|||||||
@ -111,11 +111,11 @@ class RobotsNode(BaseNode):
|
|||||||
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
|
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
|
||||||
loader = AsyncChromiumLoader(f"{base_url}/robots.txt")
|
loader = AsyncChromiumLoader(f"{base_url}/robots.txt")
|
||||||
document = loader.load()
|
document = loader.load()
|
||||||
if "ollama" in self.llm_model.model_name:
|
if "ollama" in self.llm_model.model:
|
||||||
self.llm_model.model_name = self.llm_model.model_name.split("/")[-1]
|
self.llm_model.model = self.llm_model.model.split("/")[-1]
|
||||||
model = self.llm_model.model_name.split("/")[-1]
|
model = self.llm_model.model.split("/")[-1]
|
||||||
else:
|
else:
|
||||||
model = self.llm_model.model_name
|
model = self.llm_model.model
|
||||||
try:
|
try:
|
||||||
agent = robots_dictionary[model]
|
agent = robots_dictionary[model]
|
||||||
|
|
||||||
@ -146,4 +146,4 @@ class RobotsNode(BaseNode):
|
|||||||
self.logger.warning("\033[32m(Scraping this website is allowed)\033[0m")
|
self.logger.warning("\033[32m(Scraping this website is allowed)\033[0m")
|
||||||
|
|
||||||
state.update({self.output[0]: is_scrapable})
|
state.update({self.output[0]: is_scrapable})
|
||||||
return state
|
return state
|
||||||
@ -1,61 +1,74 @@
|
|||||||
"""
|
|
||||||
Module for the tests
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import pytest
|
import pytest
|
||||||
from scrapegraphai.graphs import SmartScraperGraph
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from scrapegraphai.models import Ollama
|
||||||
|
from scrapegraphai.nodes import RobotsNode
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def sample_text():
|
def mock_llm_model():
|
||||||
"""
|
mock_model = MagicMock()
|
||||||
Example of text fixture.
|
mock_model.model = "ollama/llama3"
|
||||||
"""
|
mock_model.__call__ = MagicMock(return_value=["yes"])
|
||||||
file_name = "inputs/plain_html_example.txt"
|
return mock_model
|
||||||
curr_dir = os.path.dirname(os.path.realpath(__file__))
|
|
||||||
file_path = os.path.join(curr_dir, file_name)
|
|
||||||
|
|
||||||
with open(file_path, 'r', encoding="utf-8") as file:
|
|
||||||
text = file.read()
|
|
||||||
|
|
||||||
return text
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def graph_config():
|
def robots_node(mock_llm_model):
|
||||||
"""
|
return RobotsNode(
|
||||||
Configuration of the graph fixture.
|
input="url",
|
||||||
"""
|
output=["is_scrapable"],
|
||||||
return {
|
node_config={"llm_model": mock_llm_model, "headless": False}
|
||||||
"llm": {
|
|
||||||
"model": "ollama/mistral",
|
|
||||||
"temperature": 0,
|
|
||||||
"format": "json",
|
|
||||||
"base_url": "http://localhost:11434",
|
|
||||||
},
|
|
||||||
"embeddings": {
|
|
||||||
"model": "ollama/nomic-embed-text",
|
|
||||||
"temperature": 0,
|
|
||||||
"base_url": "http://localhost:11434",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def test_scraping_pipeline(sample_text, graph_config):
|
|
||||||
"""
|
|
||||||
Test the SmartScraperGraph scraping pipeline.
|
|
||||||
"""
|
|
||||||
smart_scraper_graph = SmartScraperGraph(
|
|
||||||
prompt="List me all the news with their description.",
|
|
||||||
source=sample_text,
|
|
||||||
config=graph_config
|
|
||||||
)
|
)
|
||||||
|
|
||||||
result = smart_scraper_graph.run()
|
def test_robots_node_scrapable(robots_node):
|
||||||
|
state = {
|
||||||
|
"url": "https://perinim.github.io/robots.txt"
|
||||||
|
}
|
||||||
|
|
||||||
assert result is not None
|
# Mocking AsyncChromiumLoader to return a fake robots.txt content
|
||||||
# Additional assertions to check the structure of the result
|
robots_node.AsyncChromiumLoader = MagicMock(return_value=MagicMock(load=MagicMock(return_value="User-agent: *\nAllow: /")))
|
||||||
assert isinstance(result, dict) # Assuming the result is a dictionary
|
|
||||||
assert "news" in result # Assuming the result should contain a key "news"
|
# Execute the node
|
||||||
assert "is_scrapable" in result
|
result_state, result = robots_node.execute(state)
|
||||||
assert isinstance(result["is_scrapable"], bool)
|
|
||||||
assert result["is_scrapable"] is True
|
# Check the updated state
|
||||||
# Ensure the execute method was called once
|
assert result_state["is_scrapable"] == "yes"
|
||||||
mock_execute.assert_called_once_with(initial_state)
|
assert result == ("is_scrapable", "yes")
|
||||||
|
|
||||||
|
def test_robots_node_not_scrapable(robots_node):
|
||||||
|
state = {
|
||||||
|
"url": "https://twitter.com/home"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Mocking AsyncChromiumLoader to return a fake robots.txt content
|
||||||
|
robots_node.AsyncChromiumLoader = MagicMock(return_value=MagicMock(load=MagicMock(return_value="User-agent: *\nDisallow: /")))
|
||||||
|
|
||||||
|
# Mock the LLM response to return "no"
|
||||||
|
robots_node.llm_model.__call__.return_value = ["no"]
|
||||||
|
|
||||||
|
# Execute the node and expect a ValueError because force_scraping is False by default
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
robots_node.execute(state)
|
||||||
|
|
||||||
|
def test_robots_node_force_scrapable(robots_node):
|
||||||
|
state = {
|
||||||
|
"url": "https://twitter.com/home"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Mocking AsyncChromiumLoader to return a fake robots.txt content
|
||||||
|
robots_node.AsyncChromiumLoader = MagicMock(return_value=MagicMock(load=MagicMock(return_value="User-agent: *\nDisallow: /")))
|
||||||
|
|
||||||
|
# Mock the LLM response to return "no"
|
||||||
|
robots_node.llm_model.__call__.return_value = ["no"]
|
||||||
|
|
||||||
|
# Set force_scraping to True
|
||||||
|
robots_node.force_scraping = True
|
||||||
|
|
||||||
|
# Execute the node
|
||||||
|
result_state, result = robots_node.execute(state)
|
||||||
|
|
||||||
|
# Check the updated state
|
||||||
|
assert result_state["is_scrapable"] == "no"
|
||||||
|
assert result == ("is_scrapable", "no")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
pytest.main()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user