|
| 1 | +from typing import List, Optional, Union |
| 2 | +from multiprocessing import Process, Queue |
| 3 | + |
| 4 | +from scrapy.spiders import Spider |
| 5 | + |
| 6 | +from llama_index.core.readers.base import BasePydanticReader |
| 7 | +from llama_index.core.schema import Document |
| 8 | + |
| 9 | +from .utils import run_spider_process, load_scrapy_settings |
| 10 | + |
| 11 | + |
| 12 | +class ScrapyWebReader(BasePydanticReader): |
| 13 | + """ |
| 14 | + Scrapy web page reader. |
| 15 | +
|
| 16 | + Reads pages from the web. |
| 17 | +
|
| 18 | + Args: |
| 19 | + project_path (Optional[str]): The path to the Scrapy project for |
| 20 | + loading the project settings (with middlewares and pipelines). |
| 21 | + The project path should contain the `scrapy.cfg` file. |
| 22 | + Settings will be set to empty if path not specified or not found. |
| 23 | + Defaults to "". |
| 24 | +
|
| 25 | + metadata_keys (Optional[List[str]]): List of keys to use |
| 26 | + as document metadata from the scraped item. Defaults to []. |
| 27 | +
|
| 28 | + keep_keys (bool): Whether to keep metadata keys in items. |
| 29 | + Defaults to False. |
| 30 | +
|
| 31 | + """ |
| 32 | + |
| 33 | + project_path: Optional[str] = "" |
| 34 | + metadata_keys: Optional[List[str]] = [] |
| 35 | + keep_keys: bool = False |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + project_path: Optional[str] = "", |
| 40 | + metadata_keys: Optional[List[str]] = [], |
| 41 | + keep_keys: bool = False, |
| 42 | + ): |
| 43 | + super().__init__( |
| 44 | + project_path=project_path, |
| 45 | + metadata_keys=metadata_keys, |
| 46 | + keep_keys=keep_keys, |
| 47 | + ) |
| 48 | + |
| 49 | + @classmethod |
| 50 | + def class_name(cls) -> str: |
| 51 | + return "ScrapyWebReader" |
| 52 | + |
| 53 | + def load_data(self, spider: Union[Spider, str]) -> List[Document]: |
| 54 | + """ |
| 55 | + Load data from the input spider. |
| 56 | +
|
| 57 | + Args: |
| 58 | + spider (Union[Spider, str]): The Scrapy spider class or |
| 59 | + the spider name from the project to use for scraping. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + List[Document]: List of documents extracted from the web pages. |
| 63 | +
|
| 64 | + """ |
| 65 | + if not self._is_spider_correct_type(spider): |
| 66 | + raise ValueError( |
| 67 | + "Invalid spider type. Provide a Spider class or spider name with project path." |
| 68 | + ) |
| 69 | + |
| 70 | + documents_queue = Queue() |
| 71 | + |
| 72 | + config = { |
| 73 | + "keep_keys": self.keep_keys, |
| 74 | + "metadata_keys": self.metadata_keys, |
| 75 | + "settings": load_scrapy_settings(self.project_path), |
| 76 | + } |
| 77 | + |
| 78 | + # Running each spider in a separate process as Scrapy uses |
| 79 | + # twisted reactor which can only be run once in a process |
| 80 | + process = Process( |
| 81 | + target=run_spider_process, args=(spider, documents_queue, config) |
| 82 | + ) |
| 83 | + |
| 84 | + process.start() |
| 85 | + process.join() |
| 86 | + |
| 87 | + if documents_queue.empty(): |
| 88 | + return [] |
| 89 | + |
| 90 | + return documents_queue.get() |
| 91 | + |
| 92 | + def _is_spider_correct_type(self, spider: Union[Spider, str]) -> bool: |
| 93 | + return not (isinstance(spider, str) and not self.project_path) |
0 commit comments