How sukhcha-in/dart_web_scraper works
Project Type and Purpose
• This is a Dart library providing a configurable and reusable web scraper. It allows users to scrape web pages or APIs through a key/value based configuration (configMap) and built‐in parsing logic.
• The library supports multiple scraping features (HTTP requests, parsing HTML through CSS selectors, JSON parsing, data cleaning, concurrency, etc.), so the design must emphasize its modular and configuration-driven nature.
Main Components
a. Library API / Entry Points:
• The primary entry is the WebScraper class (as shown in lib/dart_web_scraper.dart and related files) which exposes the scrape() method.
• That consumers import and use this class.
b. Configuration Management:
• ConfigMap structure which holds:
– Domain keys (e.g., "quotes.toscrape.com", "example.com")
– Under each domain:
▪ A list of Config objects.
▪ Each Config contains two main aspects:
• A Map of Parsers keyed by urltarget name.
• A list of UrlTargets.
• This configuration component is critical as it directs how different parts of the scraping process are executed.
c. URL Targeting:
• The UrlTarget component (and the associated UrlCleaner) determines which portions of a website or API should be scraped.
• How UrlTargets direct traffic to specific parsing logic based on URL matching ('where' list).
d. Parsing Components:
• Detail the various Parser types (e.g., element, attribute, text, table, json, etc.). Each parser is defined in its own module under lib/dart_web_scraper/parsers.
– Indicate that parsers are the heart of the extraction logic.
– Include connections that show how a Parser is triggered as part of a UrlTarget’s execution.
• Incorporate the Optional logic modules (under lib/dart_web_scraper/optional) which provide additional operations such as appending, cropping, regex matching, and more.
• Mention data injection via the <slot> mechanism for dynamic configuration of selectors and URLs.
e. Common Utilities and Models:
• Core shared functionalities and data types are maintained in lib/common.
– Models: Represent different data structures (e.g., ConfigModel, UrltargetModel, ParserModel, etc.).
– Enums: Defined in common/enums.dart to standardize parser types and other constants.
– Utility functions: For cookie handling, debug logging, HTTP processing, random number utilities, etc.
• These utilities support the consistency and reusability of the core scraping logic.
f. External Interactions:
• Data cleaning and post-processing: Represent how data is cleaned before it becomes the final output.
Relationships and Data Flow
• From WebScraper, illustrate a flow branching into two separate processes:
– If the URL matches one of the UrlTargets defined in the config: direct to the appropriate parsing workflow.
• That each Config object organizes:
– A set of UrlTargets that filter URLs.
– A mapping to Parsers that extract data from the fetched HTML/JSON.
– Data goes from WebScraper to URL targeting.
– Then data flows into the parsers.
– Utilities are used as supporting modules throughout the parsing and data refining stages.
Architectural Patterns & Design Principles
• Modular design: Each directory (config, parsers, optional functions, and common utilities) is a self-contained module.
• Separation of concerns: Distinct components for scraping, parsing, and configuration (e.g., the clear separation between Config, UrlTarget, and Parser classes).
• Extensibility: The design uses plug-in style optional methods (e.g., Optional.any, Optional.http) that allow for dynamic behaviors without modifying the core.