SmartScraper is a scraping pipeline that automates the process of extracting information from web pages using a natural language model to interpret and answer prompts. Attributes: prompt (str): The prompt for the graph. source (str): The source of the graph.
| 23 | |
| 24 | |
| 25 | class SmartScraperGraph(AbstractGraph): |
| 26 | """ |
| 27 | SmartScraper is a scraping pipeline that automates the process of |
| 28 | extracting information from web pages |
| 29 | using a natural language model to interpret and answer prompts. |
| 30 | |
| 31 | Attributes: |
| 32 | prompt (str): The prompt for the graph. |
| 33 | source (str): The source of the graph. |
| 34 | config (dict): Configuration parameters for the graph. |
| 35 | schema (BaseModel): The schema for the graph output. |
| 36 | llm_model: An instance of a language model client, configured for generating answers. |
| 37 | embedder_model: An instance of an embedding model client, |
| 38 | configured for generating embeddings. |
| 39 | verbose (bool): A flag indicating whether to show print statements during execution. |
| 40 | headless (bool): A flag indicating whether to run the graph in headless mode. |
| 41 | |
| 42 | Args: |
| 43 | prompt (str): The prompt for the graph. |
| 44 | source (str): The source of the graph. |
| 45 | config (dict): Configuration parameters for the graph. |
| 46 | schema (BaseModel): The schema for the graph output. |
| 47 | |
| 48 | Example: |
| 49 | >>> smart_scraper = SmartScraperGraph( |
| 50 | ... "List me all the attractions in Chioggia.", |
| 51 | ... "https://en.wikipedia.org/wiki/Chioggia", |
| 52 | ... {"llm": {"model": "openai/gpt-3.5-turbo"}} |
| 53 | ... ) |
| 54 | >>> result = smart_scraper.run() |
| 55 | ) |
| 56 | """ |
| 57 | |
| 58 | def __init__( |
| 59 | self, |
| 60 | prompt: str, |
| 61 | source: str, |
| 62 | config: dict, |
| 63 | schema: Optional[Type[BaseModel]] = None, |
| 64 | ): |
| 65 | super().__init__(prompt, config, source, schema) |
| 66 | |
| 67 | self.input_key = "url" if source.startswith("http") else "local_dir" |
| 68 | |
| 69 | # for detailed logging of the SmartScraper API set it to True |
| 70 | self.verbose = config.get("verbose", False) |
| 71 | |
| 72 | def _create_graph(self) -> BaseGraph: |
| 73 | """ |
| 74 | Creates the graph of nodes representing the workflow for web scraping. |
| 75 | |
| 76 | Returns: |
| 77 | BaseGraph: A graph instance representing the web scraping workflow. |
| 78 | """ |
| 79 | if self.llm_model == "scrapegraphai/smart-scraper": |
| 80 | from ..integrations.scrapegraph_py_compat import extract as sgai_extract |
| 81 | |
| 82 | response = sgai_extract( |
no outgoing calls