Scrapling Python Library

repository·main·Indexed 13 days ago

https://github.com/d4vinci/scrapling

An adaptive web scraping framework for Python (v0.4.14) providing tools for single requests and large-scale crawls. It features adaptive parsing to handle website changes and stealthy fetchers—including Fetcher, DynamicFetcher, and StealthyFetcher—to bypass anti-bot protections like Cloudflare. The library includes a Spider class for concurrent crawling, support for browser automation via Playwright, and the ability to capture background XHR data.

Tokens
146.7K
Snippets
361
Records
456
Agent score
98%

What's inside Scrapling

  1. Overview of the Scrapling MCP Server

    main

    The Scrapling MCP server provides ten tools via the Model Context Protocol (MCP) to perform web scraping and automation. It offers three distinct levels of scraping capability:

    1. Plain HTTP: Fast, lightweight requests using get or bulk_get.
    2. Browser-rendered: Executes JavaScript using fetch or bulk_fetch.
    3. Stealth/Anti-bot bypass: Uses advanced techniques to evade detection via stealthy_fetch or bulk_stealthy_fetch.

    Key features include:

    • CSS-selector-based content narrowing: Reduces token usage by extracting only specific elements before returning data.
    • Persistent browser session management: Allows creating, listing, and closing sessions.
    • Visual feedback: Supports capturing page screenshots as image content blocks.
  2. Overview of Scrapling features

    main

    Scrapling is an adaptive web scraping framework designed to handle everything from simple requests to large-scale crawls. Key capabilities include:

    • Adaptive Parsing: The parser learns from website changes and automatically relocates elements when page structures are updated.
    • Anti-Bot Bypass: Fetchers are designed to natively bypass anti-bot systems like Cloudflare Turnstile.
    • Spider Framework: Enables scaling to concurrent, multi-session crawls with features like pause/resume and automatic proxy rotation.
    • High Performance: Supports ultra-fast crawls with real-time statistics and streaming.
  3. Overview of Scrapling

    main

    Scrapling is an adaptive web scraping framework designed for everything from single requests to large-scale crawls.

    Key capabilities include:

    • Adaptive Parsing: The parser learns from website changes and can automatically re-locate elements if a page structure updates.
    • Anti-Bot Evasion: Fetchers are designed to bypass anti-bot systems like Cloudflare Turnstile out of the box.
    • Spider Framework: Supports parallel multi-session crawls with pause/resume functionality and automatic proxy rotation.
    • Performance: Designed for high-speed crawls with real-time statistics and streaming support.
  4. Key Capabilities of the Scrapling MCP Server

    main

    The Scrapling MCP Server includes several advanced features designed for robust web automation:

    • Smart Content Extraction: Converts pages/elements to Markdown, HTML, or clean text.
    • Anti-Bot Bypass: Handles Cloudflare Turnstile, Interstitial, and other protections.
    • Proxy Support: Enables anonymity and geo-targeting.
    • Browser Impersonation: Mimics real browsers via TLS fingerprinting and matching HTTP headers.
    • Parallel Processing: Concurrent scraping of multiple URLs.
    • Session Persistence: Reuses browser sessions for better performance.
    • Ad Blocking: Automatically blocks ~3,500 known ad and tracker domains to save tokens and speed up loads.
    • Prompt Injection Protection: Automatically sanitizes hidden content (CSS-hidden, aria-hidden, zero-width characters, HTML comments, etc.) to prevent injection attacks.
  5. Scrapling performance benchmarks

    main

    Scrapling is optimized for high-speed parsing and element discovery. Benchmarks comparing text extraction speed (using 5000 nested elements) and element similarity/text search performance show Scrapling outperforming several popular libraries:

    Text Extraction Speed (5000 nested elements):

    • Scrapling: 1.99 ms (Baseline)
    • Parsel/Scrapy: 2.06 ms
    • Raw Lxml: 2.56 ms
    • PyQuery: 23.98 ms
    • Selectolax: 197.02 ms
    • MechanicalSoup: 1545.15 ms
    • BS4 with Lxml: 1562.1 ms
    • BS4 with html5lib: 3412.73 ms

    Element Similarity & Text Search Performance:

    • Scrapling: 2.3 ms (Baseline)
    • AutoScraper: 12.58 ms

    Note: All benchmarks represent averages of 100+ runs.

    | # |      Library      | Time (ms) | vs Scrapling | 
    |---|:-----------------:|:---------:|:------------:|
    | 1 |     Scrapling     |   1.99    |     1.0x     | 
    | 2 |   Parsel/Scrapy   |   2.06    |    1.035     | 
    | 3 |     Raw Lxml      |   2.56    |    1.286     | 
    | 4 |      PyQuery      |   23.98   |     ~12x     | 
    | 5 |    Selectolax     |  197.02   |     ~99x     | 
    | 6 |  MechanicalSoup   |  1545.15   |   ~776.5x    | 
    | 7 |    BS4 with Lxml  |  1562.1    |   ~785.0x    | 
    | 8 | BS4 with html5lib |  3412.73   |   ~1714.9x   |
  6. Developer experience and tools

    main

    Scrapling is designed for high-performance and ease of use:

    • Interactive Shell: An optional IPython-integrated shell with shortcuts, including the ability to convert curl commands into Scrapling requests.
    • CLI Usage: Extract data from a URL directly from the terminal without writing Python code.
    • Rich Navigation API: Advanced DOM traversal (parents, siblings, children) and optimized text processing (regex, cleaning).
    • Automatic Selector Generation: Generate robust CSS/XPath selectors for any element.
    • Type Safety: Full type hint coverage for excellent IDE autocompletion and error checking (validated with PyRight and MyPy).
    • Docker Support: Official Docker images are released with every version, containing all necessary browsers.
  7. Querying elements in Scrapling

    main

    Scrapling supports parsing HTML pages (XML is not supported due to adaptive feature limitations). There are five primary ways to locate elements:

    1. CSS3 Selectors: Implements the W3C specification via cssselect.
    2. XPath Selectors: Implemented via lxml.
    3. Filters/Conditions: Finding elements based on specific criteria.
    4. Text Content: Finding elements containing specific text.
    5. Regex: Finding elements whose content matches a regular expression.

    Additionally, Scrapling supports finding elements similar to a given element (see Finding Similar Elements).

  8. Overview of Scrapling Fetchers and Session Management

    main

    Scrapling provides multiple ways to fetch web content depending on the target's complexity:

    • Fetcher: Fast, stealthy HTTP requests with browser impersonation (TLS fingerprint, headers, HTTP/3).
    • DynamicFetcher: Full browser automation using Playwright (Chromium) or Google Chrome.
    • StealthyFetcher: Advanced anti-bot bypass and fingerprint spoofing (e.g., Cloudflare Turnstile/Interstitial).
    • Session Management: Use FetcherSession, StealthySession, or DynamicSession to maintain cookies and state across requests.
    • Proxy & Network: Built-in ProxyRotator (cyclic or custom), DNS-over-HTTPS (DoH) to prevent leaks, and domain/ad blocking.
    • Remote Browsers: Connect to existing browsers via cdp_url or specify a custom executable_path.
    • Background API Capture: Use capture_xhr to automatically collect all matching XHR/fetch responses into response.captured_xhr.
  9. Overview of Scrapling Spiders Framework

    main

    Scrapling includes a full crawling framework inspired by Scrapy. Key capabilities include:

    • Spider API: Define spiders using start_urls and async parse callbacks with Request/Response objects.
    • Concurrency & Throttling: Configurable concurrency, per-domain throttling, and AutoThrottle which dynamically adjusts delays based on website responsiveness.
    • Persistence: Supports Pause & Resume via checkpoint-based crawl persistence (use Ctrl+C for graceful shutdown).
    • Streaming: Use async for item in spider.stream() to process items in real-time.
    • Templates: Includes CrawlSpider (rule-based), SitemapSpider, XMLFeedSpider/CSVFeedSpider, and ShopifySpider (for Shopify JSON APIs).
    • Exporting: Built-in exporters for JSON, JSONL, CSV, and XML via methods like result.items.to_json().
  10. Configure session-level and request-level arguments

    main

    In Scrapling, you can configure arguments globally for a session or individually for each request.

    Global Session Configuration: Arguments set in the session class apply to all requests made within that session.

    Individual Request Configuration: You can override session settings by passing specific arguments during a request. The following arguments can be configured at the browser tab/request level:

    • google_search
    • timeout
    • wait
    • page_action
    • page_setup
    • extra_headers
    • disable_resources
    • wait_selector
    • wait_selector_state
    • network_idle
    • load_dom
    • blocked_domains
    • proxy
    • selector_config
    |      Argument       | Description                                                                                                                                                                                                                         | Optional |
    |:-------------------:|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:--------:|
    |         url         | Target url                                                                                                                                                                                                                          |    ❌     |
    |      headless       | Pass `True` to run the browser in headless/hidden (**default**) or `False` for headful/visible mode.                                                                                                 |    ✔️    |
    |  disable_resources  | Drop requests for unnecessary resources for a speed boost. Requests dropped are of type `font`, `image`, `media`, `beacon`, `object`, `imageset`, `texttrack`, `websocket`, `csp_report`, and `stylesheet`.                         |    ✔️    |
    |       cookies       | Set cookies for the next request.                                                                                                                                                                                                   |    ✔️    |
    |      useragent      | Pass a useragent string to be used. **Otherwise, the fetcher will generate and use a real Useragent of the same browser and version.**                                                                               |    ✔️    |
    |    network_idle     | Wait for the page until there are no network connections for at least 500 ms.                                                                                                                        |    ✔️    |
    |      load_dom       | Enabled by default, wait for all JavaScript on page(s) to fully load and execute (wait for the `domcontentloaded` state).                                                                                             |    ✔️    |
    |       timeout       | The timeout (milliseconds) used in all operations and waits through the page. The default is 30,000 ms (30 seconds).                                                                                             |    ✔️    |
    |        wait         | The time (milliseconds) the fetcher will wait after everything finishes before closing the page and returning the `Response` object.                                                                             |    ✔️    |
    |     page_action     | Added for automation. Pass a function that takes the `page` object, runs after navigation, and does the necessary automation.                                                                                      |    ✔️    |
    |     page_setup      | A function that takes the `page` object, runs before navigation. Use it to register event listeners or routes that must be set up before the page loads.                                                     |    ✔️    |
    |    wait_selector    | Wait for a specific css selector to be in a specific state.                                                                                                                                          |    ✔️    |
    |     init_script     | An absolute path to a JavaScript file to be executed on page creation for all pages in this session.                                                                                             |    ✔️    |
    | wait_selector_state | Scrapling will wait for the given state to be fulfilled for the selector given with `wait_selector`. _Default state is `attached`._                                                              |    ✔️    |
    |    google_search    | Enabled by default, Scrapling will set a Google referer header.                                                                                                                                     |    ✔️    |
    |    extra_headers    | A dictionary of extra headers to add to the request. _The referer set by `google_search` takes priority over the referer set here if used together._                                                                |    ✔️    |
    |        proxy        | The proxy to be used with requests. It can be a string or a dictionary with only the keys 'server', 'username', and 'password'.                                                                              |    ✔️    |
    |     real_chrome     | If you have a Chrome browser installed on your device, enable this, and the Fetcher will launch and use an instance of your browser.                                                                                |    ✔️    |
    |       locale        | Specify user locale, for example, `en-GB`, `de-DE`, etc. Locale will affect `navigator.language` value, `Accept-Language` request header value, as well as number and date formatting rules. Defaults to the system default locale. |    ✔️    |
    |     timezone_id     | Changes the timezone of the browser. Defaults to the system timezone.                                                                                                                             |    ✔️    |
    |       cdp_url       | Instead of launching a new browser instance, connect to this CDP URL to control real browsers through CDP.                                                                                             |    ✔️    |
    |    user_data_dir    | Path to a User Data Directory, which stores browser session data like cookies and local storage. The default is to create a temporary directory. **Only Works with sessions**                                   |    ✔️    |
    |     extra_flags     | A list of additional browser flags to pass to the browser on launch.                                                                                                                             |    ✔️    |
    |   additional_args   | Additional arguments to be passed to Playwright's context as additional settings, and they take higher priority than Scrapling's settings.                                                                              |    ✔️    |
    |   selector_config   | A dictionary of custom parsing arguments to be used when creating the final `Selector`/`Response` class.                                                                                                            |    ✔️    |
    |   blocked_domains   | A set of domain names to block requests to. Subdomains are also matched (e.g., `
  11. Manage multiple requests with FetcherSession

    main

    Use the FetcherSession class to make multiple HTTP requests while maintaining a shared configuration, connection pool, and cookie persistence. FetcherSession automatically detects whether it is being used in synchronous or asynchronous code. It is significantly faster than creating a new session for every request and is more resource-efficient.

    When to use FetcherSession:

    • Making multiple requests to the same or different sites.
    • Maintaining cookies or authentication state between requests.
    • Utilizing connection pooling for performance.
    • Requiring consistent configuration (like impersonate or http3) across a series of requests.
    from scrapling.fetchers import FetcherSession
    
    # Create a session with default configuration
    with FetcherSession(
        impersonate='chrome',
        http3=True,
        stealthy_headers=True,
        timeout=30,
        retries=3
    ) as session:
        # All requests share the same session, cookies, and connection pool
        page1 = session.get('https://scrapling.requestcatcher.com/get')
        page2 = session.post('https://scrapling.requestcatcher.com/post', data={'key': 'value'})
        page3 = session.get('https://api.github.com/events')
  12. Build complete crawlers with Spiders

    main

    The Spider class allows you to build robust, concurrent crawlers.

    Key Features:

    • Concurrency: Set concurrent_requests to control speed.
    • Parsing: Implement an async def parse(self, response: Response) method to extract data and yield results or follow links.
    • Multi-Session Support: Use configure_sessions(self, manager) to register different session types (e.g., 'fast' for normal pages and 'stealth' for protected ones). Use the sid parameter in Request to select a session.
    • Checkpointing: Pass a crawldir to the Spider constructor to enable pause/resume functionality. Pressing Ctrl+C saves progress automatically.
    from scrapling.spiders import Spider, Request, Response
    from scrapling.fetchers import FetcherSession, AsyncStealthySession
    
    class MultiSessionSpider(Spider):
        name = "multi"
        start_urls = ["https://example.com/"]
        
        def configure_sessions(self, manager):
            manager.add("fast", FetcherSession(impersonate="chrome"))
            manager.add("stealth", AsyncStealthySession(headless=True), lazy=True)
        
        async def parse(self, response: Response):
            for link in response.css('a::attr(href)').getall():
                if "protected" in link:
                    yield Request(link, sid="stealth")
                else:
                    yield Request(link, sid="fast", callback=self.parse)
    
    # To enable pause/resume:
    # QuotesSpider(crawldir="./crawl_data").start()