# souzadevinicius/scrapling architecture

> Architecture diagram of the GitHub repository souzadevinicius/scrapling, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/souzadevinicius/scrapling
- Repository: https://github.com/souzadevinicius/scrapling

## Overview

##### Project Type and Purpose
- Scrapling as an open-source Python web scraping library.
- Its purpose is to allow users to fetch web pages stealthily (bypassing anti-bot measures), parse HTML documents with resilience against structure changes, and offer advanced selection techniques (e.g., auto-matching, CSS/XPath, text/regex filtering).

##### High-Level Components
A. User Interface / Entry Points
– Command-line interface (CLI module) and Python API.
– Represented by “scrapling/cli.py” and the “defaults” module used to import Fetcher classes.

B. Fetching Layer
– Main Fetcher classes: Fetcher, AsyncFetcher, StealthyFetcher, and PlayWrightFetcher.
– Show these as separate but related components that abstract HTTP requests.
– Each Fetcher wraps third-party libraries (for example, httpx, Camoufox, or Playwright) and configures options for stealth mode, retries, etc.

C. Engine Layer
– “scrapling/engines” contains various engines (camo, pw, static) and the “toolbelt” submodule with bypass scripts.
– Represent these as components that act as plugins or adapters for different request strategies.

D. Core Parsing and Processing Layer
– “scrapling/core” holds the central parsing logic: custom types, mixins, storage adaptors, translator, and utility functions.
– Illustrate that once a page is fetched, the raw content is passed into the core for parsing and element abstraction (creating the “Adaptor” objects).
– Indicate subcomponents handling element navigation (parent, children, siblings traversal), auto-matching (saving and later relocating element properties), and content-based search methods (regex, CSS, XPath).

E. Storage and Auto-matching Subsystem
– While not a separate directory itself, the README makes clear that the library automatically saves unique properties of HTML elements to enable later auto-matching.
– Show the data flow: when an element is first selected with auto_save enabled, its unique properties are stored; later, when auto_match is requested, the stored data is used to relocate the element.

F. Testing and Documentation
– These indicate quality control and developer guidance.

G. Integration Points/External Services
– Represent integration with third-party libraries (httpx, Camoufox, Playwright, possibly orjson for JSON operations) and even external services or browsers if real_chrome is used.
– Optionally include sponsorships as a note on how further integrations (for proxies, for example) might appear.

##### Relationships and Interactions
– Include lines indicating dependencies on external libraries or services (e.g., httpx, Camoufox, Playwright) adjacent to the appropriate fetchers.

##### Architectural Patterns and Design Principles
– Emphasize the Adapter/Facade pattern: the fetchers hide the complexities of underlying request engines and adapt the responses to a standardized “Adaptor” object.
– Highlight modularity and separation of concerns: distinct directories for core functionality, engine-specific operations, CLI, and tests.
– Note the extensibility: the “toolbelt” and engine submodules are designed to be easily extended for additional bypass techniques.

##### Additional Elements

- The entry point (CLI/API usage) → fetching layer (with various Fetcher classes routing through engines) → core parsing and processing (generating Adaptor objects, applying selectors and auto-matching) → storage (maintaining element properties) and testing/documentation support.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface / Entry Points
    subgraph "User Interface / Entry Points"
        UI1["CLI Module"]:::ui
        UI2["API Defaults"]:::ui
    end

    %% Fetching Layer
    subgraph "Fetching Layer"
        FCH["Fetcher Classes"]:::fetch
    end

    %% Engine Layer
    subgraph "Engine Layer"
        ENG["Various Engines & Toolbelt"]:::engine
    end

    %% Core Parsing & Processing Layer
    subgraph "Core Parsing & Processing Layer"
        CP["Parsing Core / Adaptor"]:::core
        ST["Auto-matching Storage"]:::storage
    end

    %% Testing & Documentation
    subgraph "Testing & Documentation"
        TEST["Automated Tests"]:::test
        DOC["Documentation"]:::docs
    end

    %% Integration / External Dependencies
    subgraph "Integration / External Dependencies"
        EXT["External Libraries/Services"]:::external
    end

    %% Connections
    UI1 & UI2 -->|"request"| FCH
    FCH -->|"routes_to"| ENG
    FCH -->|"sends_fetched_content"| CP
    ENG -->|"adapts_data_for"| CP
    CP -->|"stores_properties"| ST
    ST -->|"retrieves_properties"| CP
    UI1 -->|"triggers_via_CLI"| TEST
    UI2 -->|"triggers_via_API"| TEST
    TEST --> DOC
    FCH ---|"depends_on"| EXT
    ENG ---|"integrates_with"| EXT

    %% Click Events
    click UI1 "https://github.com/souzadevinicius/scrapling/blob/main/scrapling/cli.py"
    click UI2 "https://github.com/souzadevinicius/scrapling/blob/main/scrapling/defaults.py"
    click FCH "https://github.com/souzadevinicius/scrapling/blob/main/scrapling/fetchers.py"
    click ENG "https://github.com/souzadevinicius/scrapling/tree/main/scrapling/engines"
    click CP "https://github.com/souzadevinicius/scrapling/tree/main/scrapling/core"
    click ST "https://github.com/souzadevinicius/scrapling/blob/main/scrapling/core/storage_adaptors.py"
    click TEST "https://github.com/souzadevinicius/scrapling/tree/main/tests"
    click DOC "https://github.com/souzadevinicius/scrapling/tree/main/docs"
    click EXT "https://github.com/souzadevinicius/scrapling/tree/main/scrapling/engines/toolbelt/bypasses"

    %% Styles
    classDef ui fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef fetch fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef engine fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef core fill:#FFFFE0,stroke:#000,stroke-width:2px;
    classDef storage fill:#FFCCCB,stroke:#000,stroke-width:2px;
    classDef test fill:#D3D3D3,stroke:#000,stroke-width:2px;
    classDef docs fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef external fill:#E6E6FA,stroke:#000,stroke-width:2px;
```
