A node responsible for checking if a website is scrapeable or not based on the robots.txt file. It uses a language model to determine if the website allows scraping of the provided path. This node acts as a starting point in many scraping workflows, preparing the state with the nec
| 14 | |
| 15 | |
| 16 | class RobotsNode(BaseNode): |
| 17 | """ |
| 18 | A node responsible for checking if a website is scrapeable or not based on the robots.txt file. |
| 19 | It uses a language model to determine if the website allows scraping of the provided path. |
| 20 | |
| 21 | This node acts as a starting point in many scraping workflows, preparing the state |
| 22 | with the necessary HTML content for further processing by subsequent nodes in the graph. |
| 23 | |
| 24 | Attributes: |
| 25 | llm_model: An instance of the language model client used for checking scrapeability. |
| 26 | force_scraping (bool): A flag indicating whether scraping should be enforced even |
| 27 | if disallowed by robots.txt. |
| 28 | verbose (bool): A flag indicating whether to show print statements during execution. |
| 29 | |
| 30 | Args: |
| 31 | input (str): Boolean expression defining the input keys needed from the state. |
| 32 | output (List[str]): List of output keys to be updated in the state. |
| 33 | node_config (dict): Additional configuration for the node. |
| 34 | force_scraping (bool): A flag indicating whether scraping should be enforced even |
| 35 | if disallowed by robots.txt. Defaults to True. |
| 36 | node_name (str): The unique identifier name for the node, defaulting to "Robots". |
| 37 | """ |
| 38 | |
| 39 | def __init__( |
| 40 | self, |
| 41 | input: str, |
| 42 | output: List[str], |
| 43 | node_config: Optional[dict] = None, |
| 44 | node_name: str = "RobotNode", |
| 45 | ): |
| 46 | super().__init__(node_name, "node", input, output, 1) |
| 47 | |
| 48 | self.llm_model = node_config["llm_model"] |
| 49 | |
| 50 | self.force_scraping = ( |
| 51 | False if node_config is None else node_config.get("force_scraping", False) |
| 52 | ) |
| 53 | self.verbose = ( |
| 54 | True if node_config is None else node_config.get("verbose", False) |
| 55 | ) |
| 56 | |
| 57 | def execute(self, state: dict) -> dict: |
| 58 | """ |
| 59 | Checks if a website is scrapeable based on the robots.txt file and updates the state |
| 60 | with the scrapeability status. The method constructs a prompt for the language model, |
| 61 | submits it, and parses the output to determine if scraping is allowed. |
| 62 | |
| 63 | Args: |
| 64 | state (dict): The current state of the graph. The input keys will be used to fetch the |
| 65 | |
| 66 | Returns: |
| 67 | dict: The updated state with the output key containing the scrapeability status. |
| 68 | |
| 69 | Raises: |
| 70 | KeyError: If the input keys are not found in the state, indicating that the |
| 71 | necessary information for checking scrapeability is missing. |
| 72 | KeyError: If the large language model is not found in the robots_dictionary. |
| 73 | ValueError: If the website is not scrapeable based on the robots.txt file and |
no outgoing calls